我注意到在使用jquery的$.post()时,默认的contentType是application/x-www-form-urlencoded,但我的asp.net mvc代码需要有contentType=application/json(参见此问题以了解为什么必须使用application/json:ASPNET MVC - Why is ModelState.IsValid false "The x field is required" when that field does have a value?)。我该如何使$.post()发送contentType=application/json?我已经有大量的$.post()函数,所以不想改用$.ajax(),因为这需要太多时间。如果我尝试
那样做可以起作用,但会影响我所有的$.get和$.post请求,并导致一些请求出错。那么有没有办法改变$.post()的行为,使其发送contentType=application/json?
$.post(url, data, function(), "json")
我仍然拥有contentType=application/x-www-form-urlencoded。那么,如果“json”参数不能将contenttype更改为json,它到底是做什么的?
如果我尝试
$.ajaxSetup({
contentType: "application/json; charset=utf-8"
});
那样做可以起作用,但会影响我所有的$.get和$.post请求,并导致一些请求出错。那么有没有办法改变$.post()的行为,使其发送contentType=application/json?
$.ajaxSetup({ contentType: "application/json; charset=utf-8" });
。 - Sagnalrac