【发布时间】:2012-02-22 10:54:36
【问题描述】:
我正在尝试使用 jquery 在间隔后 ping 服务器。我发现互联网上的 jquery ajax 函数为这么多人工作,但它抛出异常 “访问受限 URI 被拒绝”代码:Firefox 中的“1012”,IE 中的错误代码 0 和 www.google.com 的 404 Chrome。我不明白问题出在哪里。以下是我的代码。
<html>
<head>
<title>Server availability</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var req = function () {
count = count + 1;
var link = $('#testURL').val();
$.ajax({
url: testURL,
success: function () {
$('#spnStatus').html($('#spnStatus').html() + '<br/><br/>' + link + ' Checked ' + count + ' times. Server available');
},
error: function (xhr, ajaxOptions, thrownError) {
//alert(xhr.status + "\n" + thrownError);
$('#spnStatus').html($('#spnStatus').html() + '<br/><br/>' + link + ' Checked ' + count
+ ' times. Server not available. Error Code:' + xhr.status + "<br/> Exception:" + thrownError);
}
});
//Call it again.
setTimeout(function () {
req();
}, 5000);
};
var count = 0;
$(document).ready(function () {
req();
});
</script>
</head>
<body>
<input id="testURL" type="text" value="http://www.google.com" /><br />
<span id="spnStatus">Not checked yet.</span>
</body>
</html>
提前致谢。
【问题讨论】:
-
是的,但它不能那样工作:S
-
您不能使用 AJAX 访问来自其他域的页面,除非它们是 JSONP 类型的。因此,如果您无法将 JSONP 页面放在 other 域(因为您可能无法控制它),则只能使用服务器端代理(local) 检查另一个页面,然后你 AJAX 到那个本地代理 .. 阅读jquery-howto.blogspot.com/2009/04/… 了解更多信息..
标签: jquery