今天突然收到感谢的评论,没想到自己随手写的问题还真能帮助到别人,还是继续自己晒鱼打网的记录吧

最近项目换了个后台合作,所以发现平时经常使用的ajax的请求方式出错

1.找了好久发现contentType一般为默认的application/x-www-form-urlencoded,这次post请求后台限定了为application/json

2.当设置contentType为application/json还是出错时,把data也要转换一下

如下:

$.ajax({
    method: "POST",
    url: "",
    contentType: 'application/json',
    data:JSON.stringify({
            "id": id
    }),
    success: function( data ) {
 
   }
});

jquery ajax 设置请求头header 参数 

$.ajax(
                {
                    url:'http://127.0.0.1:30080/api-a/quasiCustom/selectCustomList',
                    type:'post',
                    dateType:'json',
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader("organId:'1333333333'");
                    },
                    headers:{'Content-Type':'application/json;charset=utf8','organId':'1333333333'},
                    data:JSON.stringify(org),
                    success:function(data){console.log("sucess");},
                    error:function(data){console.log("error");}
                }
            );

转载于:https://wwwblogs/Anne3/p/7852456.html

               https://wwwblogs/jjSmileEveryDay/p/9636165.html 

更多推荐

jquery ajax contentType为application/json及设置请求头header