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

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");}
                }
            );

  

 

posted @ 2023-04-14 10:14  流氓大菠萝  阅读(1655)  评论(0编辑  收藏  举报