1.
$.ajax({
headers: {
"token":userToken//此处放置请求到的用户token
},
type: "POST",
url: "XXXXX",//请求url
contentType: "application/x-www-form-urlencoded",
success: (data) => {
if (data.return_code === "SUCCESS") {
this.deviceList = data.return_info;
// console.log(this.deviceList);
} else {
alert("请求错误!错误信息:" + data.return_msg);
}
},
error: (err) => {
this.loginLoading = false;
alert("错误信息:" + JSON.stringify(err))
}
})
在header里面添加参数
2.放在beforeSend的函数里面,进行数据的传输,使用XMLHttpRequest对象
$.ajax({
type: "post",
url:"http://127.0.0.1:4564/bsky-app/template/testPost",
contentType: "application/json;charset=utf-8",
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);
}
})
复制代码