Access-Control-Allow-Origin: “*” not allowed when credentials flag is true, but there is no Access-Control-Allow-Credentials header

二次信任 提交于 2019-11-27 22:53:53

"credentials flag" refers to XMLHttpRequest.withCredentials of the request being made, not to an Access-Control-Allow-Credentials header. That was the source of my confusion.

If the request's withCredentials is true, Access-Control-Allow-Origin: * can't be used, even if there is no Access-Control-Allow-Credentials header.

Requests withCredentials:true, on a server configured with Access-Control-Allow-Origin: * CAN be used, but you will need some more extra config on your server:

Using Access-Control-Allow-Origin=* on the server, it will not allow access to any resource (that requires credentials) on any xhr CORS request.

Workarounds:

  1. Make that remote resource on the server accesible without credentials ( and use xhr.withCredentials = false )
  2. Create a rewrite rule on the server, to modify the response header Access-Control-Allow-Origin=* to the request's origin. You can also apply this rewrite under certain criteria, for example, if request is using certain port or it comes from a list of whitelisted domains.

Here is some article that explains how to do this on a IIS server, but you can do this in many other servers:

PS: in case of using credentials, you will also need the following header on your server's response: Access-Control-Allow-Credentials=true

PS2: only 1 value is allowed to "access-control-allow-origin" paramenter. If you try to use for instance two domains: domain1.com domain2.com, it won't work.

I solved same problem by using these steps..

1) disable your chrome extension "Allow-Control-Allow-Origin"

2) add these into your service

var xhr = new (); xhr.withCredentials = true;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!