Javascript 如何使用 $.ajax 发送 JSON 而不是查询字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12693947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send JSON instead of a query string with $.ajax?
提问by Redsandro
Can someone explain in an easy way how to make jQuery send actual JSON instead of a query string?
有人可以简单地解释一下如何让 jQuery 发送实际的 JSON 而不是查询字符串吗?
$.ajax({
url : url,
dataType : 'json', // I was pretty sure this would do the trick
data : data,
type : 'POST',
complete : callback // etc
});
This will in fact convert your carefully prepared JSON to a query string. One of the annoying things is that any array: []in your object will be converted to array[]: [], probably because of limitations of the query sting.
这实际上会将您精心准备的 JSON 转换为查询字符串。令人讨厌的事情之一是array: []您的对象中的任何内容都将被转换为array[]: [],这可能是因为查询刺痛的限制。
回答by mekwall
You need to use JSON.stringifyto first serialize your object to JSON, and then specify the contentTypeso your server understands it's JSON. This should do the trick:
您需要先使用JSON.stringify将您的对象序列化为 JSON,然后指定contentType以便您的服务器理解它是 JSON。这应该可以解决问题:
$.ajax({
url: url,
type: "POST",
data: JSON.stringify(data),
contentType: "application/json",
complete: callback
});
Note that the JSONobject is natively available in browsers that support JavaScript 1.7 / ECMAScript 5 or later. If you need legacy support you can use json2.
请注意,该JSON对象在支持 JavaScript 1.7 / ECMAScript 5 或更高版本的浏览器中本机可用。如果您需要遗留支持,您可以使用json2。
回答by Bergi
No, the dataTypeoptionis for parsing the received data.
不,该dataType选项用于解析接收到的数据。
To post JSON, you will need to stringify it yourself via JSON.stringifyand set the processDataoption to false.
要发布 JSON,您需要自己将其字符串化并将选项JSON.stringify设置processData为false.
$.ajax({
url: url,
type: "POST",
data: JSON.stringify(data),
processData: false,
contentType: "application/json; charset=UTF-8",
complete: callback
});
Note that not all browsers support the JSONobject, and although jQuery has .parseJSON, it has no stringifier included; you'll need another polyfill library.
请注意,并非所有浏览器都支持该JSON对象,尽管 jQuery 有.parseJSON,但它不包含字符串化符;你需要另一个 polyfill 库。
回答by yardpenalty.com
While I know many architectures like ASP.NET MVC have built-in functionality to handle JSON.stringify as the contentType my situation is a little different so maybe this may help someone in the future. I know it would have saved me hours!
虽然我知道像 ASP.NET MVC 这样的许多架构都有处理 JSON.stringify 作为 contentType 的内置功能,但我的情况有点不同,所以也许这可能对将来的人有所帮助。我知道这可以节省我几个小时!
Since my http requests are being handled by a CGI API from IBM (AS400 environment) on a different subdomain these requests are cross origin, hence the jsonp. I actually send my ajax via javascript object(s). Here is an example of my ajax POST:
由于我的 http 请求是由 IBM(AS400 环境)在不同子域上的 CGI API 处理的,因此这些请求是跨域的,因此 jsonp. 我实际上是通过 javascript 对象发送我的 ajax。这是我的 ajax POST 示例:
var data = {USER : localProfile,
INSTANCE : "HTHACKNEY",
PAGE : $('select[name="PAGE"]').val(),
TITLE : $("input[name='TITLE']").val(),
HTML : html,
STARTDATE : $("input[name='STARTDATE']").val(),
ENDDATE : $("input[name='ENDDATE']").val(),
ARCHIVE : $("input[name='ARCHIVE']").val(),
ACTIVE : $("input[name='ACTIVE']").val(),
URGENT : $("input[name='URGENT']").val(),
AUTHLST : authStr};
//console.log(data);
$.ajax({
type: "POST",
url: "http://www.domian.com/webservicepgm?callback=?",
data: data,
dataType:'jsonp'
}).
done(function(data){
//handle data.WHATEVER
});
回答by Tod
If you are sending this back to asp.net and need the data in request.form[] then you'll need to set the content type to "application/x-www-form-urlencoded; charset=utf-8"
如果您将其发送回 asp.net 并需要 request.form[] 中的数据,那么您需要将内容类型设置为“application/x-www-form-urlencoded; charset=utf-8”
Original post here
原帖在这里
Secondly get rid of the Datatype, if your not expecting a return the POST will wait for about 4 minutes before failing. See here
其次摆脱数据类型,如果您不期望返回,POST 将在失败前等待大约 4 分钟。看这里

