byt*_*ave 32 jquery post jquery-post
这个真的很奇怪.我$.post()在代码中有多个,但有一个dunno为什么发送json参数x-www-form-urlencoded而不是因此不起作用.
这是代码:
$.post("/Route/SaveTransportProperties", { properties: JSON.stringify(propArray), currTravelBox: JSON.stringify(travelBoxObj), accessToken: getAccessToken()}, function(data)
{
//DO STUFF
});
Run Code Online (Sandbox Code Playgroud)
XHR在Firefox中看起来像这样:

任何想法为什么会发生这种情况?我还将类型强制为'json',但也不起作用.
Jam*_*urz 39
如果要将数据作为json发送,则使用$ .ajax函数
您可以指定type 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
},
});
Run Code Online (Sandbox Code Playgroud)
取自ajax文档
http://api.jquery.com/jQuery.ajax/
contentTypeString
Default: 'application/x-www-form-urlencoded; charset=UTF-8'
Run Code Online (Sandbox Code Playgroud)
这也适用于我
$.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
},
});
Run Code Online (Sandbox Code Playgroud)