$.ajax({
headers: {
Accept: "application/json; charset=utf-8"
},
type: "get",
success: function (data) {
}
});
$.ajax({
type: "GET",
url: "default.do",
beforeSend: function(request) {
request.setRequestHeader("Test", "Chenxizhang");
},
success: function(result) {
alert(result);
}
});
// 设置请求默认值
$.ajaxSetup({
beforeSend: function (xhr) { //可以设置自定义标头
// 将token塞进Header里
xhr.setRequestHeader('Authorization', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9');
xhr.setRequestHeader('Content-Type', 'application/json'); // application/x-www-form-urlencoded
},
complete: function (xhr) {
// 设置登陆拦截
if (xhr.responseJSON.code == "error_unauth") {
console.log("没有登录!");
layer.msg("没有登录!");
// location.href="login.html";
} else {
console.log("已经登录!");
}
},
});
或
// 设置请求默认值
$.ajaxSetup({
headers: { // 默认添加请求头
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ,
"Content-Type": "application/json"
} ,
complete: function (xhr) {
// 设置登陆拦截
if (xhr.responseJSON.code == "error_unauth") {
console.log("没有登录!");
layer.msg("没有登录!");
// location.href="login.html";
} else {
console.log("已经登录!");
}
},
});
原文地址:https://blog.csdn.net/ximaiyao1984/article/details/131186438
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_25570.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!