简体   繁体   English

如何通过AJAX调用Web服务来返回Json数据?

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

I have a web service which returns JSON data below code I am using for calling the web service. 我有一个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() {} }); }; 

it return output of alert in success function as [object object] but I want it in proper json format. 它在成功函数中以[ob​​ject object]的形式返回alert的输出,但是我希望它以正确的json格式显示。

You must read JSON.stringify() 您必须阅读JSON.stringify()

use alert(JSON.stringify(data)) 使用alert(JSON.stringify(data))

Example: 例:

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