簡體   English   中英

如何通過AJAX調用Web服務來返回Json數據?

[英]How return Json data by calling an web-service through AJAX?

我有一個Web服務,該服務在我用來調用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() {} }); }; 

它在成功函數中以[ob​​ject object]的形式返回alert的輸出,但是我希望它以正確的json格式顯示。

您必須閱讀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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM