我可以测试是否网址可以访问,使用AJAX +跨域+ JSONP? [英] Can I test if URL is reachable using AJAX + cross-domain + jsonp?

查看:448
本文介绍了我可以测试是否网址可以访问,使用AJAX +跨域+ JSONP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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");
        }
    });

这篇关于我可以测试是否网址可以访问,使用AJAX +跨域+ JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆