2012-10-01 114 views
0

我有一個奇怪的問題,我的腳本中的jQuery ajax函數。AJAX發送錯誤

我的問題only發生在我的朋友計算機上,但我並沒有受到我計算機上的任何事情的影響。

我試圖讓錯誤的功能屬性警報錯誤的內容,但事情發生了strange

$.ajax({ 
    url : EXECUTION_URL + 'ajax/users.php', 
    type : 'POST', 
    error : function(err1, err2, err3) { 
     alert(err2 + ' : ' + err3 + ' , ' + err1.responseText); 
    }, 
    data : { 
     'value' : 'val', 
     'type' : 'email' 
    }, 
    success : function(data, statusText, xhr) { 
     alert(data) 
    }, 
}); 

的重要路線是:
alert(err2 + ' : ' + err3 + ' , '+ err1.responseText)
但只火:'error: , '
爲什麼會發生這種情況?以及如何我可以知道發送此請求的錯誤

+0

使用開發工具在幾乎所有的瀏覽器或類似http://www.fiddler2.com/fiddler2/ – Joe

+0

我最大的問題是我不能看到我的電腦 –

回答

0

也許,您試圖提醒的對象可能無法轉換爲字符串(對於警報文本)。嘗試使用console.log。 然後你就會知道發生了什麼,你就可以解決這個問題。

+0

毫米毫米的錯誤,我認爲所有其中的字符串由jquery默認我會嘗試console.log –

+0

至少err1不能是一個字符串,因爲你正在訪問它的一個元素(.responseText) – alexandernst

+0

err1是一個對象,err1.responseText是一個文本?或對象? –

0

你的錯誤功能

error : function(err1, err2, err3) { // is equivalent to 

error : function(jqXHR, textStatus, errorThrown) 

Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". 

也許ERR2爲空,這就是你看不到輸出ERR2

1

嘗試使用$.ajaxSetup()得到正確的錯誤是這樣的原因:

$(function() { 
    $.ajaxSetup({ 
     error: function(jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       alert('Not connect.\n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       alert('Requested page not found. [404]'); 
      } else if (jqXHR.status == 500) { 
       alert('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       alert('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       alert('Time out error.'); 
      } else if (exception === 'abort') { 
       alert('Ajax request aborted.'); 
      } else { 
       alert('Uncaught Error.\n' + jqXHR.responseText); 
      } 
     } 
    }); 
}); 

刪除你的ajax調用中的錯誤函數,然後調用你的ajax方法。如果出現一些錯誤,ajax設置會捕獲它並根據發生的錯誤顯示正確的警報。