jQuery $.post() 不会将数据作为 json 发送,而是作为 x-www-form-urlencoded 发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5529685/
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
$.post() doesn't send data as json but as x-www-form-urlencoded instead
提问by byte_slave
This one is really weird. I've multiple $.post()
in the code but there is one dunno why sends the json parameters as x-www-form-urlencoded
instead and therefore doesn't work.
这个真的很奇怪。我$.post()
在代码中有多个,但有一个不知道为什么将 json 参数x-www-form-urlencoded
改为发送,因此不起作用。
Here's the code:
这是代码:
$.post("/Route/SaveTransportProperties", { properties: JSON.stringify(propArray), currTravelBox: JSON.stringify(travelBoxObj), accessToken: getAccessToken()}, function(data)
{
//DO STUFF
});
The XHR looks like this in Firefox:
XHR 在 Firefox 中看起来像这样:
Any ideas why is this happening? I also enforced the type as 'json' but doesn't work either.
任何想法为什么会发生这种情况?我也将类型强制为“json”,但也不起作用。
回答by James Kyburz
If you want to send the data as json then use the $.ajax function
如果要将数据作为 json 发送,请使用 $.ajax 函数
You can specify type post and dataType json.
您可以指定类型 post 和 dataType json。
$.ajax({
url: "mydomain.com/url",
type: "POST",
dataType: "xml/html/script/json", // expected format for response
contentType: "application/json", // send as JSON
data: $.param( $("Element or Expression") ),
complete: function() {
//called when complete
},
success: function() {
//called when successful
},
error: function() {
//called when there is an error
},
});
Taken from ajax documentation
取自 ajax 文档
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
contentTypeString
Default: 'application/x-www-form-urlencoded; charset=UTF-8'
回答by Olli
Because $.post()is for sending form-like requests. $.ajaxis for sending whatever you want to. See contentType
in $.ajax
page for more information.
因为$.post()用于发送类似表单的请求。$.ajax用于发送您想要的任何内容。请参见contentType
在$.ajax
页面了解更多信息。
Quote:
引用:
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases. If you explicitly pass in a content-type to $.ajax() then it'll always be sent to the server (even if no data is sent). Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side.
向服务器发送数据时,请使用此内容类型。默认为“application/x-www-form-urlencoded”,这在大多数情况下都适用。如果您明确地将内容类型传递给 $.ajax() ,那么它将始终发送到服务器(即使没有发送数据)。数据将始终使用 UTF-8 字符集传输到服务器;您必须在服务器端对其进行适当的解码。
回答by mohammadali ghanbari
this also works for me
这也适用于我
$.ajax({
url: "mydomain.com/url",
type: "POST",
dataType: "xml/html/script/json", // expected format for response
contentType: "application/json", // send as JSON
data: JSON.stringify(data),
complete: function() {
//called when complete
},
success: function() {
//called when successful
},
error: function() {
//called when there is an error
},
});
回答by obotezat
you can also force your data to be a json in the success function:
data = JSON.parse(data);
您还可以在成功函数中强制您的数据为 json:
data = JSON.parse(data);