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

标签 javascript jquery json 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() {}
});
};

它将成功函数中警报的输出返回为 [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/

相关文章:

javascript - 在 JS 中使用 CSS

javascript - 在 typescript svelte 中导入别名 svelte 组件

javascript - 从嵌套对象数组中递归创建字符串?

javascript - jquery/js 语法对象数组

iOS json识别

php - file_get_contents ("php://input") 在 var_dump 时显示为空

javascript - Firefox:在确认弹出窗口时停止传播回车键?

javascript - 上传到服务器后水平 slider 无法正常工作

javascript - jqGrid 更改默认警报文本

python - 在 Flask 模板中编码 JSON