使用Ajax跨域請求資源,Nginx作為代理,出現:The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed 錯誤。
服務端允許跨域配置:
#region 設置允許跨域,允許復雜請求
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
//HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
#endregion
Nginx的配置:
add_header 'Access-Control-Allow-Origin' '*';
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
return 200;
}
proxy_pass http://xx:8002/;
#proxy_pass http://localhost:62249/;
contains multiple values "*" 意思就是設置了2次跨域,但是只有一個是允許的,移除其中的任意一個就好了。如果服務器設置了允許跨域,使用Nginx代理里面就不需要了(或者就不用使用Nginx了)
