ajax请求里的contentType: "application/json"作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax请求里的contentType: "application/json"作用相关的知识,希望对你有一定的参考价值。

对于页面中复杂数据的提交:

$.ajax({
type:"post",
url:url,
dataType: "json",
contentType: "application/json",
async:true,
data:JSON.stringify(param),
success:function(result){
if(result.code == "0"){
var rows = result.data;
if(typeof successcallback == ‘function‘){successcallback(rows);}else{alert(result.msg);}
}else{
alert(result.msg);
}
},
error:function(jqXHR, textStatus, errorThrown){
//alert("提交失败,请重试!");
}
});

需要通过JSON.stringify(param)把页面中获取的参数变成json字符串,,此时需要加上contentType: "application/json",




















以上是关于ajax请求里的contentType: "application/json"作用的主要内容,如果未能解决你的问题,请参考以下文章

Ajax跨域之ContentType为application/json请求失败的问题

Ajax请求中,contentType和dataType的区别

ajax请求中contentType与dataType区别

jQuery Ajax GET 和 contentType?

$.ajax 中的contentType

$.ajax 中的contentType 坑坑