如何使用 vanilla JS 发送 JSON 数据的 AJAX POST 请求。
我知道内容类型是 url 格式编码的,它不支持嵌套的 JSON。
有什么方法可以在普通的旧 JS 中使用嵌套 JSON 发出这样的 POST 请求。我已经尝试过在 SO 上找到的各种序列化方法,但它们都将我的 JSON 扁平化为一种格式。
这是我的 JSON:
{
email: "hello@user.com",
response: {
name: "Tester"
}
}
最佳答案
如果你正确使用 JSON,你可以毫无问题地嵌套对象:
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
var theUrl = "/json-handler";
xmlhttp.open("POST", theUrl);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({ "email": "hello@user.com", "response": { "name": "Tester" } }));
https://stackoverflow.com/questions/39519246/
相关文章:
c# - 在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?
c# - 将日期时间从 javascript 传递给 c# (Controller)
json - 如何对 Windows Azure 动态内容启用 gzip HTTP 压缩
c# - 使用 JsonConvert.DeserializeObject 将 Json 反序列化为
ruby-on-rails - 使用 Rspec,如何在 Rails 3.0.11 中测试我的 Co
javascript - 如何将 JavaScript 对象编码为 JSON?
c# - Json.Net 中的 PreserveReferencesHandling 和 Refe
asp.net - 我可以将 JSON 字符串转换为 JsonResult 吗?