我使用jQuery来从URL中读取信息,并在我的网页显示它是异步的。该URL来自其他领域,所以我用JSONP来获取数据。这工作得很好。
I'm using JQuery to fetch information from an URL and display it on my page asynchronously. The URL comes from other domain, so I use JSONP to get the data. That works fine.
然而,当远程URL下跌(这在一段时间发生一次),我的网页挂作为的JQuery AJAX 不叫'成功'或'错误'的功能。
However, when the remote URL is down (which happens once in a while) my page hangs as JQuery AJAX doesn't call the 'success' or 'error' functions.
我使用jQuery 1.7。
I'm using JQuery 1.7.
我的code是这样的:
My code looks like:
$.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的不可达的话,我看不到任何东西。
If "somePage" is up, then I see the message "ok". If "somePage" is not reachable, then I don't see anything.
我如何能得到任何想法错误函数被调用?或者更重要的是,如何检测,如果跨域网址可以访问?
Any ideas on how can I get "error" function get called? Or more importantly, how to detect if the cross-domain URL is reachable?
是,即使可能吗?
谢谢
添加一个 暂停
$.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");
}
});