【问题标题】:Response Header not accessible in JS在 JS 中无法访问响应标头
【发布时间】:2021-08-03 16:15:47
【问题描述】:

我在前端使用 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'));
            });

    };
};
...

这里的参数对象是空的

可能是什么问题?

【问题讨论】:

  • 这能回答你的问题吗? Read Authorization header from response
  • 如果您使用 JSP,那么您可能正在从 JSP 变量中读取标头值。在某些情况下,JSP 变量甚至在设置标头之前就读取它的值。
  • 感谢您的回复@phuzi,但没有帮助。
  • 感谢您的回复@shubham。它没有帮助。

标签: javascript spring authentication redux axios


【解决方案1】:

Access-Control-Expose-Headers:授权

response.headers.get('授权')

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

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2016-02-13
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多