1

We have two domains(xyz.com and zxy.com). and using jquery ajax call to get the data from one domain.

If first one fails, how do i check and redirect to another domain using javascript.

user655334
  • 965
  • 3
  • 15
  • 27
  • You say you have an ajax call that gets data. Where is your code? There is an 'error:' handler for ajax calls in jQuery. Did you google? Check this http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages – Klaas Leussink Sep 18 '12 at 12:51

1 Answers1

3

Could you not just use window.location.host to find out which domain the user is currently on?

This way you can make the correct ajax call from the start. There is no reason to make a request to the wrong domain, wait for it to fail then make another request.


If you do however wish to actually make an AJAX request and wait for it to fail then make a different request it would be as follows (this answer is quite verbose on purpose):

var firstRequest = $.ajax({
  url: 'http://xyz.com',
  timeout: 5000
});

firstRequest.done(function (data) {

});

firstRequest.fail(function () {

  var anotherRequest = $.ajax({
    url: 'http://xzy.com',
    timeout: 5000
  });

  anotherRequest.done(function (data) {

  });

  anotherRequest.fail(function () {

  });

});
Sphvn
  • 5,247
  • 8
  • 39
  • 57