-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){});
        }
    });
});
Community
  • 1
  • 1
vishnu
  • 3
  • 2

2 Answers2

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;        
}
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 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. – techfoobar Sep 22 '12 at 07:09
0

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

radu florescu
  • 4,315
  • 10
  • 60
  • 92