如果找不到主机,Ajax重定向

ajax redirect if host not found

本文关键字:Ajax 重定向 主机 找不到 如果      更新时间:2023-09-26

如果主机不可达,我如何重定向客户端页面?我的代码工作,如果主机是可访问的,但不做什么,当它不是:

$(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'); 
    }
}

     });
});    

尝试使用错误函数。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");
    }
  });
});