-2

Possible Duplicate:
jquery (ajax) redirect to another domain

I have the jQuery Ajax request in the following format:

$.ajax({
        url: 'xyz.com',
        cache: false,
        success: function(data){}
      );

If the request fails(server not responds), how do i redirect to another domain.

I did the same thing by using jquery getJson in the follwing way:

$.getJSON('xyz.com',function(result) {})
 .error(
    function(xhr, textStatus, errorThrown){
        if (xhr.status == 500) {
            $.getJSON('zxy.com', function(result){});
        }
    });
});
3
  • window.location.replace("stackoverflow.com"); Commented Sep 22, 2012 at 6:53
  • @vishnu I'm not quite sure your question is understandable Commented Sep 22, 2012 at 6:55
  • This answer will help you. Commented Sep 22, 2012 at 6:56

2 Answers 2

1

Demo: http://jsfiddle.net/dYgfG/

Code:

var domain = "http://xyz.com";

try {
    $.ajax({
        url: domain,
        cache: false,
        success: function(data){},
        error: function() {
            alert('ajax error: so redirecting');
            redirectUser();
        }
    });
}
catch(e) {
    alert('exception: so redirecting');
    redirectUser();
}

function redirectUser() {
    window.location.href = domain;        
}
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. New user? If the answer helped you, mark it as 'accepted' using the green tick next to the title of the answer.
0

I think you should use : crossDomain property for ajax. Check jquery api link

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.