【问题标题】:jquery.ajax ping with head methodjquery.ajax ping 与 head 方法
【发布时间】:2013-10-08 13:34:09
【问题描述】:

我正在做一个检查多个网站是否在线的项目。我尝试使用以下代码检查一个知名网站,作为试用,但它一直返回错误。我该如何解决?

function Ping() {
    $.ajax({
        type: "HEAD",
        url: "http://www.google.com",
        success: function(result){
            alert('reply');
        },
        error: function (result) {
            alert('failure');
        }
    });
}
window.setInterval(Ping, 10000);

【问题讨论】:

  • 你很可能会被Same Origin Policy 打败,这意味着你的success 处理程序永远不会被击中。
  • 感谢您的回复。那么我该如何解决这个问题。我想检查任何网站是否在线或离线。提前致谢。
  • 解决方法是使用本地服务器端代理来执行此操作,然后通过 jQuery 进行查询。

标签: jquery ajax ping head


【解决方案1】:

使用 javascript 的服务器端代理的替代方法是使用您希望 ping 的 url 加载图像 (new Image()) 并检查图像的 onload/onerror 事件中的状态。

例如:

var img = new Image();
img.onload = function(){
// the ip/domain is online
}

img.onerror = function(e){ 
// the ip/domain is online
}

img.src = 'http://www.google.co.in'; // the ip/domain to ping

完整的例子在这里: http://jsfiddle.net/GSSCD/203/

这不是一个完整的解决方案,但需要进一步实施;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多