1、登录页面提交用户名和密码后,获得了token,并保存在了localStorage和store中
2、在axios封装文件http.js中设置了请求拦截器,代码如下
//添加请求拦截器
axios.interceptors.request.use(config => {
if (localStorage.getItem('token')) {
config.headers.Authorization = localStorage.getItem('token');
}
return config;
},error => {
return Promise.reject(error);
});
问题:刷新页面后,console提示如下错误
Access to XMLHttpRequest at 'http://127.0.0.1/api/v1.info/list?page=1&limit=10' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
把请求拦截器的这段代码去掉,就是正常的
if (localStorage.getItem('token')) {
config.headers.Authorization = localStorage.getItem('token');
}
而且跨域问题,在php后端已经设置过下面代码了
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
不知道是什么原因,求解?