Aks*_*pal 68 javascript ajax json
如何使用vanilla JS发送一个AJAX POST请求发送JSON数据.
我理解内容类型是url表单编码,它不支持嵌套的JSON.
有没有什么办法可以在普通的旧JS中使用嵌套的JSON来发出这样的POST请求.我已经尝试了SO上的各种序列化方法,但它们都将我的JSON压缩成一种格式.
这是我的JSON:
{
email: "hello@user.com",
response: {
name: "Tester"
}
}
Run Code Online (Sandbox Code Playgroud)
Gil*_*not 175
如果正确使用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" } }));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
115657 次 |
最近记录: |