2017-06-28 175 views
0

我有一個Web服務,它返回JSON數據,下面我用來調用Web服務的代碼。如何通過AJAX調用Web服務來返回Json數據?

jQuery.ajax({ 
 
    url: 'http://localhost:5606/xyz', 
 
    type: "POST", 
 
    contentType: "application/json; charset=utf-8", 
 
    dataType: 'json', 
 
    data: '{"a":"b"}', 
 
    success: function(responses, textStatus, XMLHttpRequest) { 
 
    alert(responses); 
 
    }, 
 
    error: function(xhr, err) { 
 
    console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status); 
 
    console.log("responseText: " + xhr.responseText); 
 
    }, 
 
    complete: function() {} 
 
}); 
 
};

其返回警報輸出成功的功能像[目標對象]但我想它在正確的JSON格式。

+1

使用'的console.log()''不alert' – guradio

+0

但我想響應值調用另一個webservice –

+1

這就是要點。您可以獲取JSON數據,不要「提醒」它,只需將其記錄在控制檯中即可。 –

回答

1

您必須閱讀JSON.stringify()

使用alert(JSON.stringify(data))

例子:

var response = {}; 

response.status ="success"; 
response.data="Your data"; 

alert(response); //It will give you [object object] 
console.log(response); //Gives JSON data in console 
alert(JSON.stringify(response)); //Alerts json string 

if(response.status == "success") 
    //Pass response.data to the next webservice it will still be in the json format. 
+0

我的回答是'{「responses」:[{「coordinate_search」:{「actions」:[]},「took」:67,「timed_out」:false,「_ shards」:{「total」:5, 「:5,」 失敗 「:0},」 命中 「:{」 總 「:15500」,MAX_SCORE 「:0.0,」 命中 「:[]},」 聚合 「:{」 2 「:{」 doc_count_error_upper_bound「: 0,「sum_other_doc_count」:0,「桶」:[{「key」:263731936070,「doc_count」:34},{「key」:263735261942,「doc_count」:26}' 在這裏我只想要值「鑰匙」 –

+0

您是指桶內的關鍵屬性? –

+0

$。每個(response.aggregations.2.bucket,功能(鍵,值) { 如果(鍵== '鍵') value.key; //這裏獲取鍵值 } ); –

相關問題