How to get front-end data in response headers and request headers inside?

Today encounter a difficult problem for the front-end look ignorant white is forced. The problem is how to obtain the token in response to head inside for token presumably everyone is familiar with, but to get inside the head token response was the first time. Before After token is returned by the backend to the frontend to hear this demand, hearts full steam ahead. The next step is how to get from axios vue in response header inside the token:


   axios.interceptors.response.use( response => {  //axios拦截器
    if (response.status === 200) {  //响应成功后
        if(response.headers['Authorization']){  //获取响应头里面的数据,**Authorization根据你响应头里面的数据获取,并不是唯一值**
            downLoad(response.data,response.headers['Authorization'].split(';')[1].split('=')[1]);
        }
        return Promise.resolve(response);
    } else {
        return Promise.reject(response);
    }
    }, error => {  //报错后的处理,这里不是重点,
    // 服务器状态码不是200的情况
    if (error.response.status) {
        switch (error.response.status) {
            case 401:
            Notification.error({
                title: '错误',
                message: '登录过期,请重新登录'
            });
        // 清除token
        localStorage.removeItem('token');
        store.commit('loginSuccess', null);
        setTimeout(() => {
            router.replace({
                path: '/login',
                query: {
                    redirect: router.currentRoute.fullPath
                }
            });
        }, 1000);
    break;
    case 404:
        Notification.error({
            title: '错误',
            message: '网络请求不存在',
        });
    break;
    case 504:
        Notification.error({
            title: '错误',
            message: '服务器内部异常',
        });
    break;
    // 其他错误,直接抛出错误
    default:
        Notification.error({
            title: '错误',
            message: error.response.data.message
        });
}
    return Promise.reject(error.response);

    }
});

For details, see axios link https://www.kancloud.cn/yunye/axios/234845

Change a day bug, a big growth

Guess you like

Origin www.cnblogs.com/taoyuanju/p/11361110.html