后端已经允许了跨域,前端依然报跨域错误,研究一下 Access-Control-Allow-Credentials

即使后端已经允许了跨域,但是前端依然报一个跨域错误。

Access to XMLHttpRequest at 'http://localhost/api/admin/authorizations' from origin 'http://localhost:9528' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

尝试了很多网上的方法也都没有弄清原因在哪里。研究一下 Access-Control-Allow-Credentials 这个头的作用,果然药到病除。这个是服务端下发到客户端的 response 中头部字段,意义是允许客户端携带验证信息,例如 cookie 之类的。这样客户端在发起跨域请求的时候,不就可以携带允许的头,还可以携带验证信息的头,又由于客户端是请求框架是 axios,并且手残的设置了 withCredentials: true,意思是客户端想要携带验证信息头,但是我的服务端设置是 'supportsCredentials' => false, ,表示不允许携带信息头,好了,错误找到了。

我们的客户端和服务端交互的时候使用的是 token,通过 Authorization头发送到服务端,并没有使用到 cookie,所以客户端没有必要设置 withCredentials: true,一顿操作猛如虎🤣🤣。

原文:https://www.jianshu.com/p/ea485e5665b3

posted @ 2019-05-17 14:01  叨叨的蜗牛  阅读(7509)  评论(0编辑  收藏  举报