2014-09-04 124 views
1

我有一個AJAX調用:的Javascript AJAX返回錯誤

  $('#testyo').click(function(){ 
      $.ajax({ 
       type: 'GET', 
       url: "../messages/drew", 
       dataType: 'JSON', 
       success:function(data){ 
       alert(data); 
       }, 
       error: function(data) 
       { 
        console.log(data); 
        alert("Error: "+data); 
       } 
      }); 
      return false; 
      }); 

它應該是成功的,但我得到的警報( 「錯誤:」 +數據)警報。數據是[對象對象]。因此警報只是說錯誤:[對象的對象]

在我的console.log(數據)

Object {readyState: 4, 
getResponseHeader: function, 
getAllResponseHeaders: function, 
setRequestHeader: function, 
overrideMimeType: function…} 
abort: function (statusText) {always: function() {complete: function() {done: function() {error: function() {fail: function() {getAllResponseHeaders: function() {getResponseHeader: function (key) {overrideMimeType: function (type) {pipe: function (/* fnDone, fnFail, fnProgress */) {progress: function() {promise: function (obj) {readyState: 4responseText: "drew"setRequestHeader: function (name, value) {arguments: nullcaller: nulllength: 2name: ""prototype: Object__proto__: function Empty() {}<function scope>state: function() {status: 200statusCode: function (map) {statusText: "OK"success: function() {arguments: nullcaller: nulllength: 0name: ""prototype: Object__proto__: function Empty() {}<function scope>then: function (/* fnDone, fnFail, fnProgress */) {__proto__: Object 

正如你可以看到它確實表明了responseText的:「畫」,這是我想要的。我只是想知道爲什麼要通過我的失敗功能,而不是我的成功。請讓我知道是否還有其他需要幫助我解決的問題。

+3

使用'console.log'在javascript控制檯中打印錯誤並檢查您的對象。 – Alvaro 2014-09-04 13:57:09

+0

這不是我所做的嗎? – user3591126 2014-09-04 13:59:41

+0

收到的HTTP代碼是什麼? – Fractaliste 2014-09-04 14:01:27

回答

1

根據jQuery的文檔,所述誤差參數有三個輸入作爲參數:

error:

Type: Function(jqXHR jqXHR, String textStatus, String errorThrown) 

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

的errormessage的,並且可以如果修改的誤差函數取三要觀看的錯誤參數輸入並訪問第二個參數以知道錯誤字符串是什麼。

如果你這樣做,很容易發現錯誤並知道請求失敗的原因。一旦發現錯誤,修復它就變得很容易。

unexpected token可能意味着你已經從服務器得到了損壞的JSON響應。還要確保服務器組件發送的響應是JSON類型。

設置服務器響應類型,例如,如果響應是JSON:

response.setContentType("application/json"); 

另請參見:

What is the correct JSON content type?

參見:http://api.jquery.com/jquery.ajax/

+0

謝謝你。我的錯誤現在說「SyntaxError:意外的令牌d」 – user3591126 2014-09-04 14:12:33

+0

是的,我JSON被損壞。修正了它,並修復了它。謝謝! – user3591126 2014-09-04 14:25:41

+0

@ user3591126 - 歡迎光臨! – BatScream 2014-09-04 14:30:00

0

我看到你得到200響應,但可能你的服務器返回錯誤的內容類型fo r json或者完全無效的json。

在firebug或chrome的Firefox中,嘗試檢查響應和響應頭像內容類型。

https://stackoverflow.com/a/11112320/1641941

0

當你responseText回報 「吸引」,你的反應是不是JSON,但字符串。刪除dataType: 'JSON'和jQuery將使用自動檢測。然後success函數將被調用。

JSON應該是類似{"myText": "drew"}