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

当后端设置允许跨域,前端仍报跨域错误时,问题可能出在Access-Control-Allow-Credentials头。该头允许客户端携带认证信息,如cookie。在axios中误设置为true,而服务端未允许携带认证头,导致冲突。解决方法是根据实际需求调整服务端配置,避免携带认证信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

转载于:https://www.cnblogs.com/i6010/articles/10880961.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值