1、登录页面提交用户名和密码后,获得了token,并保存在了localStorage和store中2、在axios封装文件http.js中设置了请求拦截器,代码如下//添加请求拦截器axios.interceptors.request.use(config=>{if(localStorage.getItem('token')){config.headers.Authorization=localStorage.getItem('token');}returnconfig;},error=>{returnPromise.reject(error);});问题:刷新页面后,console提示如下错误AccesstoXMLHttpRequestat'http://127.0.0.1/api/v1.info/list?page=1&limit=10'fromorigin'http://localhost:8080'hasbeenblockedbyCORSpolicy:RequestheaderfieldauthorizationisnotallowedbyAccess-Control-Allow-Headersinpreflightresponse.把请求拦截器的这段代码去掉,就是正常的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');不知道是什么原因,求解?
2 回答
隔江千里
TA贡献1906条经验 获得超10个赞
错误信息提示的很明显RequestheaderfieldauthorizationisnotallowedbyAccess-Control-Allow-Headersinpreflightresponse.允许跨域设置中需要让authorization通过header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization')
UYOU
TA贡献1878条经验 获得超4个赞
header('Access-Control-Allow-Origin:*');header('Access-Control-Allow-Methods:POST');header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization')
添加回答
举报
0/150
提交
取消