猿问

vue 中使用token进行验证的问题

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');不知道是什么原因,求解?
哔哔one
浏览 737回答 2
2回答

隔江千里

错误信息提示的很明显RequestheaderfieldauthorizationisnotallowedbyAccess-Control-Allow-Headersinpreflightresponse.允许跨域设置中需要让authorization通过header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization')

UYOU

header('Access-Control-Allow-Origin:*');header('Access-Control-Allow-Methods:POST');header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization')
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答