第一步:
server
{
listen 80;
server_name xxx;
index ;
root /www/xxx
# 添加如下代码
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'POST,PUT,GET,DELETE';
add_header Access-Control-Allow-Headers 'version, access-token, user-token, Accept, apiAuth, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With';
}
第二步:
重启nginx
若依旧跨域并类似以下报错:
Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response
那么请在"add_header Access-Control-Allow-Headers"中添加你请求头参数
例如:
你的请求头中含有,“token”,"xx_id"等,
那么最终配置就是
listen 80;
server_name xxx;
index ;
root /www/xxx
# 添加如下代码
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'POST,PUT,GET,DELETE';
# 对比原来的多增加了"token"及"xx_id"
add_header Access-Control-Allow-Headers 'token,xx_id,version, access-token, user-token, Accept, apiAuth, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With';
}
然后重启Nginx就OK啦~!!!