
1、放在请求头中
代码如下:
$.ajax({ type: "post", url: "http:///test/getInfo", headers: { //请求头 Accept: "APPlication/json; charset=utf-8", token: "" + token //这是获取的token }, data:JSON.stringify(JSONdate), contentType: "application/json", //推荐写这个 dataType: "json", success: function(data){ console.log('ok'); }, error:function(){ console.log('error'); } })
2、使用beforeSend方法设置请求头
代码如下:
$.ajax({
type: "post",
url: "HTTP://xxx.com/onlinejudge/test/getInfoById",
beforeSend: function(request) { //使用beforeSend
request.setRequestHeader("token", token);
request.setRequestHeader("Content-Type","Application/json");
},
data:JSON.stringify(jsonDate),
dataType: "json",
success: function(data){
console.log('ok');
},
error:function(){
console.log('error');
}
})试试吧!





网友评论文明上网理性发言 已有0人参与
发表评论: