在做vue开发的时候遇到的一个小问题,本地调试的时候因为假数据是放在了服务器上,当用axios请求时会出现无法跨域请求的问题,翻阅资料得到解决方案,方法如下
Nginx服务器设置,配置文件加入
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}
如果你也是nginx环境也刚好遇到这个问题的话大家可以试试这个方法。