用自定义请求头token
为例
- 方法一
$.ajax({
type: "post",
url:"http://127.0.0.1:4564/bsky-app/template/testPost",
contentType: "application/json;charset=utf-8",//这个参数也是header参数
data :JSON.stringify({"bodyName":"sdfsdf","date":"2017-11-28 07:34:01","price": 10.5,"tbId": 1}),
dataType: "json",
//这里是重点
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxOD.....");
},
success: function (data) {
alert(data);
},error:function(error){
console.log(error);
}
});
- 方法二
$.ajax({
type: type,
headers: {
'Access-Token':$.cookie('access_token')
},
url: url,
data: data,
success: function(data) {
},
error: function(err) {
},
complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数
}
});