[英]Can I test if URL is reachable using AJAX + cross-domain + jsonp?
我正在使用JQuery從URL獲取信息並異步顯示在我的頁面上。 該URL來自其他域,因此我使用JSONP來獲取數據。 這很好。
但是,當遠程URL關閉時(偶爾發生一次),我的頁面掛起,因為JQuery AJAX沒有調用“成功”或“錯誤”函數。
我正在使用JQuery 1.7。
我的代碼看起來像:
$.ajax({
type : "GET",
url : "http://otherdomain.com/somePage.html",
data : params,
dataType : "jsonp",
jsonp : "jsonp",
success : function (response, textS, xhr) {
alert("ok");
},
error : function (xmlHttpRequest, textStatus, errorThrown) {
alert("not ok " + errorThrown);
}
});
如果“somePage”已啟動,那么我會看到消息“ok”。 如果“somePage”無法訪問,那么我什么都看不到。
關於如何獲得“錯誤”功能的任何想法被調用? 或者更重要的是,如何檢測跨域URL是否可訪問?
這甚至可能嗎?
謝謝,
添加timeout
$.ajax({
type : "GET",
url : "http://otherdomain.com/somePage.html",
data : params,
timeout:3000,
dataType : "jsonp",
jsonp : "jsonp",
success : function (response, textS, xhr) {
alert("ok");
},
error : function (xmlHttpRequest, textStatus, errorThrown) {
alert("not ok " + errorThrown);
if(textStatus==='timeout')
alert("request timed out");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.