javascript - 在 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'));
});
};
};
...
参数对象在这里是空的
可能是什么问题?
解决方案
Access-Control-Expose-Headers:授权
response.headers.get('授权')
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
推荐阅读
- c# - 在统一游戏中编写对函数的函数调用不起作用
- javascript - 如何将 jsPDF 转换后的 pdf 文件发送到后端服务器?
- java - Netbeans 8.2 主页1
- scientific-computing - COMSOL 中时间相关 PDE 中的特征值作为系数
- vuejs2 - 在 VusJS 中仅覆盖组件模板
- android - 如何在 Android Webview 的自定义错误页面中添加重试按钮?
- sql - 将平均值添加到 PostgreSQL 中每个组的列
- ruby-on-rails - 如何在 ruby on rails 中获取复选框值
- node.js - CastError:对于猫鼬中模型的路径“_id”处的值“findByName”,转换为 ObjectId 失败
- mysql - 带有关联的活动记录查询