The 'Access-Control-Allow-Origin' header contains multiple values'*, *', but only one is allowed.

忘是亡心i 2022-04-12 11:41 194阅读 0赞

使用Ajax跨域请求资源,Nginx作为代理,出现:The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *‘, but only one is allowed 错误。

服务端允许跨域配置:

  1. \#region 设置允许跨域,允许复杂请求
  2. HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "\*");
  3. if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
  4. \{
  5. HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS");
  6. HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
  7. //HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
  8. HttpContext.Current.Response.End();
  9. \}
  10. \#endregion

Nginx的配置:

  1. add\_header 'Access-Control-Allow-Origin' '\*';
  2. location / \{
  3. if ($request\_method = 'OPTIONS') \{
  4. add\_header Access-Control-Allow-Origin \*;
  5. add\_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
  6. return 200;
  7. \}
  8. proxy\_pass http://xx:8002/;
  9. \#proxy\_pass http://localhost:62249/;
  10. \}

看上面错误提示,contains multiple values “*“ 意思就是设置了2次跨域,但是只有一个是允许的,移除其中的任意一个就好了。如果服务器设置了允许跨域,使用Nginx代理里面就不需要了(或者就不用使用Nginx了)

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读