2014-04-08 52 views
0

我正在使用$ .ajax向https服務器準備的api發送jsonp請求。jsonp請求中未定義的responseText

我的腳本如下:

var sUrl = "https://localhost/api/" + document.getElementById('query').value + 
      "?apikey=c13eb63d-b9ee-4ceb-89fe-944072deddbb&fmt=json&limit=&query=" + document.getElementById('query').value; 

    $.ajax({ 
     type: 'GET', 
     url: sUrl, 
     dataType: 'jsonp', 
     jsonp: false, 
     async:false, 
     crossDomain : true, 
     complete: function(jqXHR, textStatus){ 
       alert(jqXHR); 
      alert(jqXHR.responseText); 
      } 
      }); 

我的請求將被髮送,我可以在我的瀏覽器中收到的結果,這是我的迴應主體:

{「relatedid_restriction」:空」目的 「:」 減輕」, 「ASN」:NULL, 「RIR」:NULL, 「alternativeid」:空, 「CC」:NULL, 「detecttime」: 「2014-04-08T17:34:31Z」, 「地址」 : 「google.com」, 「alternativeid_restriction」:空, 「ID」: 「2bdd4e07-26ba-4dc2-99a8-bdea54bf3d4f」, 「GUID」: 「每個人」, 「嚴重」:空, 「評價」: 「搜索」 ,「rdata」:null,「description」:「search google.com」,「asn_desc」:null,「relatedid」:null,「reporttime」:「2014-04-08T17:34:31Z」,「con fidence「:」50「,」restriction「:」private「,」prefix「:null} {」relatedid_restriction「:null,」purpose「:」mitigation「,」asn「:null,」rir「 「:空,」 CC 「:NULL,」 detecttime 「:」 2014-04-08T17:35:14Z 「 」地址「: 」google.com「, 」alternativeid_restriction「:空, 」ID「:」 1df23bea-AB15 -4410-aab4-d2c538a385fd「,」guid「:」所有人「,」嚴重性「:空,」評估「:」搜索「,」rdata「:空,」描述「:」搜索google.com「,」asn_desc「 :null,「relatedid」:null,「reporttime」:「2014-04-08T17:35:14Z」,「confidence」:「50」,「restriction」:「private」,「prefix」:null}

現在第一個提醒「alert(jqXHR);」返回[對象對象],但第二個「警報(jqXHR.responseText);」返回undefined。

如何在我的JavaScript中檢索響應正文文本?

謝謝

+1

這種反應是不是jsonp。請研究JSONP是什麼。它與你的正常ajax請求非常不同,它根本不是ajax請求。 –

回答

0

如果您使用jsonp,則需要進行函數調用。就像這樣:

echo $_GET['callback'] . "(" . json_encode($DATA) . ");"; 

因爲jsonp需要調用一個函數。我認爲jQuery將添加一個以callback作爲名稱的GET變量。

比你可以這樣做:

jsonp: "callback", 
success : function(data){ 
    console.log(data); 
} 

更多信息和示例:http://www.jquery4u.com/json/jsonp-examples/

0

從jQuery的文檔:

文本和XML類型的任何處理返回的數據。數據只是分別通過jqXHR對象的responseText或responseXML屬性傳遞給成功處理程序。

也就是說,responseText的僅當有效datatype="text",所以要麼改變數據類型,如果你不需要JSONP或使用您的完整回調的第一個參數,這應該是對象,表示您的JSON響應