2012-11-29 78 views
2

如何處理AJAX中的錯誤?如何處理jQuery中的AJAX錯誤

在我的代碼中,即使未加載departments.json文件,也不執行包含console.log的else條件。我通過刪除departments.json文件將其加載到代碼中進行檢查。

我的代碼是:

$.getJSON("departments.json?" + new Date().getTime(), {}, function(departments, status, xhr) { 
    if (xhr.status == 200) { 
     var numericDepts = []; 
     var nonNumericDepts = []; 

     for(dept in departments) { 
      $("#kss-spinner").css({'display':'none'}); 
      if (isNaN(departments[dept].depNo)) { 
       if (isNaN(parseInt(departments[dept].depNo,10))) 
        nonNumericDepts[nonNumericDepts.length] = departments[dept]; 
       else 
        numericDepts[numericDepts.length] = departments[dept]; 
      } 
      else 
       numericDepts[numericDepts.length] = departments[dept]; 
     } 

     numericDepts.sort(cmp_dept); 
     nonNumericDepts.sort(function(dept1,dept2) { 
      return dept1.depNo.toLowerCase() - dept2.depNo.toLowerCase(); 
     }); 
     departments.sort(cmp_dept); 
     var k = 0; 

     $.each(numericDepts.concat(nonNumericDepts), function() { 
      if (k % 2 == 0) { 
       $('<p class="odd" onClick="selectTag(this,\'' + this.id + '\', 1)">' + this.depNo + '</p>').appendTo($(".scroller", $("#br1"))); 
      } 
      else { 
       $('<p class="even" onClick="selectTag(this,\'' + this.id + '\', 1)">' + this.depNo + '</p>').appendTo($(".scroller", $("#br1"))); 
      } 
      k++; 
     }); 
     $("#kss-spinner").css({'display':'none'}); 
    } 
    else { 
     console.log(xhr.status); 
     console.log(xhr.response); 
     console.log(xhr.responseText) 
     console.log(xhr.statusText); 
     console.log('json not loaded'); 
    } 
}); 

回答

1

您將需要使用fail()方法,以實現這一目標。

例子:

$.get("test.php") 
    .done(function(){ alert("$.get succeeded"); }) 
    .fail(function(){ alert("$.get failed!"); }); 
4

你可以只使用通用ajax()功能:

$.ajax({ 
    url: url, 
    dataType: 'json', 
    data: data, 
    success: successCallback, 
    error: errorCallback 
}); 
+1

['jQuery.getJSON'(http://api.jquery.com/jQuery.getJSON/)其實上面的代碼減去'錯誤的短手'部分。 –

+2

@SalmanA和'error'部分是必需的。刪除文件將導致一個'404',這將意味着調用'error'函數。在OP的當前代碼中,他沒有'error'處理程序。:沒有任何反應。 –

+0

我不記得說錯誤部分是*不*必需的。 –

1

,如果你需要一個通用的錯誤處理程序使用

$.ajaxSetup({ 
      error: function(xhr, status, error) { 
      // your handling code goes here 
      } 
      }); 
1

JQuery的的getJSON功能是一種抽象超過常規.ajax() m ethod - 但它排除了錯誤回調。

基本上,您定義的函數只有在調用成功時纔會調用(這就是爲什麼它永遠不會到達else部分)。

處理錯誤,之前設置一個錯誤處理程序是這樣的:

$.ajaxError(function(event, jqXHR, ajaxSettings, thrownError) { alert("error");}); 

每當一個Ajax請求出錯完成,該函數將被調用。

你也可以在你的的getJSON通話結束追加.error:

$.getJSON("example.json", function() { 
    (...) 
}).error(function() { (...) }); 
0

$.getJSON()功能僅僅是更普遍.ajax()功能的專用版本。

.ajax()函數將爲您提供所需的額外功能(例如錯誤函數)。你可以在這裏閱讀更多的文檔http://api.jquery.com/jQuery.ajax/

$.ajax({ 
    url: "departments.json?" + new Date().getTime(), 
    dataType: 'json', 
    success: function(departments){ 
     var numericDepts = []; 
     var nonNumericDepts = []; 
     for(dept in departments) 
     { 
     $("#kss-spinner").css({'display':'none'}); 
     if(isNaN(departments[dept].depNo)) 
     { 
      if(isNaN(parseInt(departments[dept].depNo,10))) 
      nonNumericDepts[nonNumericDepts.length]=departments[dept]; 
      else 
      numericDepts[numericDepts.length]=departments[dept]; 
     } 
     else 
      numericDepts[numericDepts.length]=departments[dept]; 
     } 
     numericDepts.sort(cmp_dept); 
     nonNumericDepts.sort(function(dept1,dept2) { 
     return dept1.depNo.toLowerCase() - dept2.depNo.toLowerCase(); 
     }); 
     departments.sort(cmp_dept); 
     var k=0; 
     $.each(numericDepts.concat(nonNumericDepts),function(){ 
     if(k%2==0){ 
      $('<p class="odd" onClick="selectTag(this,\''+this.id+'\',1)">'+this.depNo+'</p>').appendTo($(".scroller",$("#br1"))); 
     } else { 
      $('<p class="even" onClick="selectTag(this,\''+this.id+'\',1)">'+this.depNo+'</p>').appendTo($(".scroller",$("#br1"))); 
     } 
     k++; 
     }); 
     $("#kss-spinner").css({'display':'none'}); 
    }, 
    error: function(xhr, textStatus, errorThrown) { 
    console.log(xhr.status); 
    console.log(xhr.response); 
    console.log(xhr.responseText) 
    console.log(xhr.statusText); 
    console.log('json not loaded'); 
    } 
});​