我有一个 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() {}
});
};
它将成功函数中警报的输出返回为 [object object],但我希望它采用正确的 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.
关于javascript - 如何通过AJAX调用Web服务返回Json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796144/