2017-08-24 78 views
-1

這裏是我的AJAX代碼:

jQuery.ajax({url: url, 
    method: 'GET', 
    data: getParams, 
    /*success: function (json, textStatus, jqXHR) { 
     if(jQuery.active <= 1){ 
      waitDialog.removeWaitDialog(); 
     } 

     createProcessButtonEvent(); 

     ajaxError = false; 
     returnFunction(jqXHR.responseJSON); 
    },*/ 
    complete: function(jqXHR, textStatus, errorThrown) { 
     if(jQuery.active <= 1){ 
      waitDialog.removeWaitDialog(); 
     } 

     createProcessButtonEvent(); 

     var response; 
     if (typeof jqXHR.responseJSON === 'object') { 
      response = jqXHR.responseJSON; 
     } else { 
      response = jqXHR.responseText; 
     } 

     if(response.errors.length == 1){ // jsonResults.message != '' 
      if(!warningMessage){ //hard coded for ad designer 
       jQuery('#alertMessageText').text(response.errors[0].message); 
       jQuery('#alertMessage').show(); 
       jQuery('#waitDialog').jqmHide(); 
       ajaxError = true; 
       return; 
      } 
      warningMessage.displayMessage(response.errors[0].message); 
      jQuery('.popup').modal('hide'); 
      jQuery('.popup').not('.persist').remove(); 
     } 
     if (response.errors.length > 1){ 
      for(var n = 0; n < response.errors.length; n++){ 
       if (response.errors[n].id == 1){ // 2005 
        window.location.href = '/login?destination=' + encodeURIComponent(window.location.pathname + window.location.search); 
       } 
       if (response.errors[n].id == 9500){ 
        statusMessage.displayMessage(response.errors[n].message); 
       } 
      } 
      ajaxError = true; 
      //if(errorFunction){ 
      // errorFunction(jsonResults); 
      //} 
      waitDialog.removeWaitDialog(); 
     } 

     if (!ajaxError) { 
      returnFunction(jqXHR.responseJSON); 
     } 
    }, 
    dataType: 'json', 
    contentType: 'application/json' 
}); 

我告訴它去http://127.0.0.1/api/parameter,其中parameter是無效的資源。我的API在新標籤中的回報:

{"errors":[{"id":3,"message":"GET route not defined for /api/parameter"}],data:{}}

我有一個500個狀態碼返回,因爲訪問無效的資源是一個錯誤。當我打電話給實際的AJAX時,我得到jqXHR.responseJSONnull,而jqXHR.responseText''

我一直在使用這兩種success:error:塊嘗試,並試圖complete:因爲它似乎像我的API是解決呼叫錯誤塊已被調用後,你可以用註釋中看到。

所以我得到TypeError: response is null,因爲我的反應對象是從來沒有填充,而奇怪的是,我到parameter?parameters=here呼叫不會提供網絡

+0

也許你已經從網絡選項卡中篩選出XHR's - 你正在使用哪種瀏覽器 - 但是,閱讀剩下的問題表明問題的標題非常具有誤導性。 –

+0

是'url'同源或異源 –

+0

@JaromandaX,你看了我的腦海!我在瀏覽器中禁用了XHR篩選,按狀態代碼排序,並查找了我的呼叫。只有200和304,沒有500.'url'是一個總是指向我的本地主機的相對URL。謝謝閱讀。 – NobleUplift

回答

1

檢查由於我有過API控制,我改變了這個具體的錯誤返回與狀態碼501未實現,而不是500,它似乎工作。看起來這是一個非常獨特的邊緣案例。

相關問題