2014-01-07 115 views
0

我有一個ajax調用,並且當發出錯誤的請求時,我一直在遇到錯誤。它確實有效,當有好的要求時。我已經嘗試過jsonp和所有這些東西,但這並沒有幫助我更進一步。如何捕獲xmlhttprequest錯誤

這是errow

XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

這是我的代碼。

var submit = function(){ 
     var url = "" 
     var data = ["9b4e749d5cb6e069dd49cab93a69","aufujishngidrg"] 
     for(var int = 0; int < data.length; int++) { 
      console.log(data[int]) 
      url = *insert url* 
      $.ajax({ 
       type:'GET', 
       url:encodeURI(url), 
       dataType: "json", 
       contentType:"application/json; charset=utf-8", 
       error:function (xmlHttpRequest,textStatus, xhr, ajaxOptions, thrownError) { 
        if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0){ 
         console.log("xmlhttpRequest error") 
         return; 
        } 
        else{ 
         console.log("error") 
        } 
       }, 
       success:function(data){ 
        console.log(data) 
       }, 
      }); 
     } 
     console.log("derp") 
    } 

我發送一個哈希碼從一個列表和服務器返回一個ID,如果它在數據庫中。

服務器端啓用了跨域通信,因此不應該成爲問題,因此良好的請求確實可行。當一個未知的散列在列表中時,我得到這個htttpxmlrequest錯誤,並且循環中斷。

問題是:如何捕獲錯誤並繼續迭代?

+0

你能解決服務器端的代碼,以便當記錄不存在,它不彈? –

+0

如果輸出錯誤,您是否可以使用'continue;'關鍵字來繼續循環循環? – lfender6445

+0

@ lfender6445不,它要麼宣揚不明確的繼續聲明錯誤,要麼根本沒有任何區別。 – runedisc

回答

0

嘗試包裝你的Ajax調用在一個try catch語句吞下錯誤

try { 
    $.ajax{ 
    error: function(xmlHttpRequest,textStatus, xhr, ajaxOptions, thrownError){ 
     throw(thrownError) 
    } 
    success: function(){} 
    } 
} catch(err) { 
    continue; 
}