2012-02-22 43 views
0

我試圖在使用jquery的時間間隔之後ping服務器我在互聯網上找到jquery ajax函數爲很多人工作,但它拋出異常 」拒絕「代碼:Firefox中的」1012「」,IE中的Errorcode 0和www.google.com的404 Chrome。我不明白是什麼問題。以下是我的代碼。訪問被限制的URI被拒絕「代碼:」1012「

<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> 

在此先感謝。

+0

可能的重複[訪問受限制的URI拒絕代碼:1012](http://stackoverflow.com/questions/51283/access-to-restricted-uri-denied-code-1012) – 2012-02-22 10:58:58

+0

是的,但它是但它不是這樣工作的:S – 2012-02-22 11:39:15

+2

您不能使用AJAX從其他域訪問頁面,除非它們是JSONP類型。所以如果你不能在JSONP頁面* *其他*域(*,因爲你可能無法控制*),你只能使用服務器端代理(*本地*)來檢查另一個頁面,而你AJAX到該本地代理..閱讀http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html更多關於.. – 2012-02-22 11:45:58

回答

0

Gaby aka G. Petrioli謝謝你的第二個評論是我的答案。 :)

相關問題