美文网首页
http持久化 token权限验证

http持久化 token权限验证

作者: squidbrother | 来源:发表于2021-03-18 22:09 被阅读0次

前端数据请求添加token验证

  • 方法1
$.ajax({
    type: "GET",
    url: "/access/logout/" + userCode,
    headers: {'Authorization': token}
});
  • 方法2 ajax提供beforeSend
$.ajax({
    type: "GET",
    url: "/access/logout/" + userCode,
    beforeSend: function(request) {
        request.setRequestHeader("Authorization", token);
    },
    success: function(result) {
    }
});
import axios from 'axios';
const http = axios.create({
    baseURL: 'https://api.example.com'
});

//Add a request interceptor -- 将token添加至请求头信息,发送至后台
http.interceptors.request.use((config)=>{
    if(localStorage.getItem('token')){
        config.headers.Authorization = localStorage.getItem('token') || '';
    }
    return config;
},(err)=>{
    return Promise.reject(err);
});

// Add a response interceptor
http.interceptors.response.use(function (response) {
    //针对响应结果做处理
    return response;
}, function (error) {
    return Promise.reject(error);
});

相关文章

网友评论

      本文标题:http持久化 token权限验证

      本文链接:https://www.haomeiwen.com/subject/rugjcltx.html