请求接口时,报错信息如下
XMLHttpRequest cannot load http://mod.pmit.cn/NewsB/GetN... A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.
如果前端请求开启
withCredentials: true
后台接口也需要对应的将Access-Control-Allow-Credentials
设为true
。其实很多人并没有真正的理解 CORS,除了
Access-Control-Allow-Methods
和Access-Control-Allow-Origin
之外还有几点要注意的:Access-Control-Allow-Headers
:如果请求头带了Access-Control-Request-Headers
相应的服务端也要加Access-Control-Allow-Headers
而且两个值相同。Access-Control-Allow-Credentials
:如果请求加了withCredentials: true
服务端也要将此设为true
。