2014-02-27 25 views
0

的代碼是這樣的:如何檢索jquery.ajax錯誤的responseText?

$.ajax({ 
    url: "http://server.com/rest", 
    dataType: "jsonp", 
    success:function(d, t, x) { 
     console.log(d); 
    }, 
    error:function(x, t, e) { 
     console.log(x.responseText); 
    } 
}); 

螢火蟲顯示我接收到響應,但上面的代碼只記錄「未定義」。 Firebug還顯示http狀態碼是200.有人可以告訴我如何檢索響應嗎?

+2

可能重複:http://stackoverflow.com/questions/19035557/jsonp-request-error-handling –

+0

jqXHR.responseText給我「未定義」 –

+0

查看鏈接發佈者@AnthonyGarcia –

回答

1

如果您使用的是jsonp。你的服務器應該將響應綁定到回調函數。 實施例

服務器響應

myFunction(someresponsehere) 

客戶端

function myFunction(response) 
{ 
    //you will get the response from server here "response" 
    } 
+0

我知道,但在這種情況下,我不控制服務器。服務器只返回沒有回調的數據,所以回調永遠不會被調用。我不能使用非jsonp調用,因爲我得到一個「No」Access-Control-Allow-Origin'標題出現在請求的資源「錯誤。我得到數據的唯一方法是如果我使用jsonp。 –

+1

@ferdtomale比你運氣不好。 – epascarello