如果找不到主机,则重定向ajax

时间:2014-08-25 19:00:15

标签: javascript jquery html ajax

如果主机无法访问,如何重定向客户端页面?如果主机可以访问,我的代码可以正常工作,但是当主机不可访问时,我的代码无效:

$(document).on('pagebeforeshow', '#index', function(){      
    $.ajax({url: "http://www.espnsdsd.com",
            dataType: "jsonp",

            complete: function(e, xhr, settings){
    if(e.status === 200){
        window.location.replace('http://www.espn.com'); 

    }else{
        window.location.replace('http://www.anotherespn.com'); 
    }
}


     });
});    

1 个答案:

答案 0 :(得分:1)

尝试使用错误功能。 http://api.jquery.com/jQuery.ajax/

$(document).on('pagebeforeshow', '#index', function(){      
  $.ajax({url: "http://www.espnsdsd.com",
    dataType: "jsonp",
    complete: function(e, xhr, settings){
      if(e.status === 200){
        window.location.replace('http://www.espn.com'); 
      }
    },
    error: function(xhr, status, err) {
      window.location.replace("http://www.anotherespn.com");
    }
  });
});