如何使用 vanilla JS 进行 AJAX POST 请求发送 JSON 数据。
我知道内容类型是 url 形式编码的,它不支持嵌套的 JSON。
有什么办法可以在普通的旧 JS 中使用嵌套的 JSON 发出这样的 POST 请求。我已经尝试了在 SO 上找到的各种序列化方法,但它们都将我的 JSON 扁平化为一种格式。
这是我的 JSON:
{
email: "hello@user.com",
response: {
name: "Tester"
}
}
如何使用 vanilla JS 进行 AJAX POST 请求发送 JSON 数据。
我知道内容类型是 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" } }));