首页 > 解决方案 > 在 JS 中无法访问响应标头

问题描述

我在前端使用 React + Redux,在后端使用 Spring。在浏览器和邮递员中查看时,响应标头包含授权标头,但在尝试使用 javascript 访问时不包含。我在网络选项卡和控制台选项卡中添加了显示响应标头的照片。而且我正在使用 axios 的请求。

在此处输入图像描述

在此处输入图像描述

auth.js

...
export const authInit = (data) => {
    return dispatch => {
        dispatch(authInitStart());
        axios.post('/login', data)
            .then(response => {
                if(response.status === 200){

                    const param = {
                        'Authorization': response.headers.Authorization
                    };
                    console.log("Authorization----" + JSON.stringify(response));
                    console.log("Param----" + JSON.stringify(param));
                    localStorage.setItem('Authorization', param['Authorization']);
                    dispatch(authInitSuccess(param['Authorization']));

                }else{
                    dispatch(authInitFail('Request failed'));
                }
            }).catch(err => {
                dispatch(authInitFail('Network error'));
            });

    };
};
...

参数对象在这里是空的

可能是什么问题?

标签: javascriptspringauthenticationreduxaxios

解决方案


Access-Control-Expose-Headers:授权

response.headers.get('授权')

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers


推荐阅读