我注意到,在jquery中使用$.post()时,默认的内容类型是application/x-www-form-urlencoded -当我的asp.net mvc代码需要有contentType=application/json
(请参阅这个问题,为什么我必须使用application/json: ASPNET MVC -为什么是ModelState。当该字段有值时,该x字段是必需的?)
我怎么能让$.post()发送contentType=应用程序/json?我已经有了大量的$.post()函数,所以我不想更改为$.ajax(),因为这会花费太多时间
如果我尝试
$.post(url, data, function(), "json")
它仍然有contentType=application/x-www-form-urlencoded。那么,如果“json”参数没有将内容类型更改为json,它究竟做了什么呢?
如果我尝试
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
这是可行的,但影响到每一美元。Get和$。帖子,我有和导致一些打破。
那么是否有一些方法,我可以改变$.post()的行为发送contentType=application/json?
美元。如果你有CORS(跨源资源共享)问题,post将无法工作。尝试使用$。Ajax的以下格式:
$.ajax({
url: someurl,
contentType: 'application/json',
data: requestInJSONFormat,
headers: { 'Access-Control-Allow-Origin': '*' },
dataType: 'json',
type: 'POST',
async: false,
success: function (Data) {...}
});
只使用
jQuery.ajax ({
url: myurl,
type: "POST",
data: mydata,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(){
//
}
});
更新@JK:如果你在你的问题中只写一个带有$的代码示例。你在答案中找到一个相应的例子。我不想重复同样的信息,你已经学习了,直到知道:$。Post和$。Get是$.ajax的缩写形式。所以只需使用$。Ajax,您可以使用它的全部参数集,而无需更改任何全局设置。
By the way I wouldn't recommend overwriting the standard $.post. It's my personal opinion, but for me it's important, not only that the program works, but also that all who read your program understand it with the same way. Overwriting standard methods without having a very important reason can follow to misunderstanding in reading of the program code. So I repeat my recommendation one more time: just use the original $.ajax form jQuery instead of jQuery.get and jQuery.post and you receive programs which not only perfectly work, but can be read by people without any misunderstandings.
这个简单的jquery API扩展(来自:https://benjamin-schweizer.de/jquerypostjson.html)为$. postjson()做的伎俩。您可以像使用其他原生jquery Ajax调用一样使用postJSON()。您可以附加事件处理程序等等。
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json; charset=utf-8',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
像其他Ajax api(如AngularJS中的$http)一样,它将正确的contentType设置为application/json。
你可以直接传递json数据(javascript对象),因为它在这里被字符串化了。
预期返回的数据类型被设置为JSON。
你可以为承诺附加jquery的默认事件处理程序,例如:
$.postJSON(apiURL, jsonData)
.fail(function(res) {
console.error(res.responseText);
})
.always(function() {
console.log("FINISHED ajax post, hide the loading throbber");
});
我最终在我的脚本中添加了以下方法:
jQuery["postJSON"] = function( url, data, callback ) {
// shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
callback = data;
data = undefined;
}
return jQuery.ajax({
url: url,
type: "POST",
contentType:"application/json; charset=utf-8",
dataType: "json",
data: data,
success: callback
});
};
然后使用它
$.postJSON('http://url', {data: 'goes', here: 'yey'}, function (data, status, xhr) {
alert('Nailed it!')
});
这是通过简单地从原始JQuery源代码中复制“get”和“post”的代码并硬编码一些参数来强制JSON post来实现的。
谢谢!
美元。如果你有CORS(跨源资源共享)问题,post将无法工作。尝试使用$。Ajax的以下格式:
$.ajax({
url: someurl,
contentType: 'application/json',
data: requestInJSONFormat,
headers: { 'Access-Control-Allow-Origin': '*' },
dataType: 'json',
type: 'POST',
async: false,
success: function (Data) {...}
});