后端使用CrossOrigin页面还显示跨域问题
后端使用@CrossOrigin页面还显示跨域问题?
后端使用@CrossOrigin页面还显示跨域问题?
前后端分离项目,后端设置的允许跨域的代码,到前端调试界面的时候还显示了不允许跨域。
错误信息如下:
Access to XMLHttpRequest at
‘http://localhost:8000/carInfo/carCondition/1/4’
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-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
解决方案:
withCredentials: false
跨域问题解决:
原因:
CORS
请求发出时,已经设定了credentials,但服务端配置了http响应首部
Access-Control-Allow-Origin
的值为通配符 ("*") ,而这与使用credentials相悖。
要在客户端改正这个问题,只需要确保发出 CORS 请求时将credential设置为false。
如果使用
XMLHttpRequest
发出请求,确保未将withCredentials
设置为 true。如果使用
Server-sent events
, 确保EventSource.withCredentials
的值为 false(false为默认值)。
如果使用
Fetch API
,确保Request.credentials
的值为 “omit”.如果还不成功,则需要修改服务端,可能需要修改 Access-Control-Allow-Origin 的值,来为客户端所能够加载资源的源予以授权。
@CrossOrigin 源码: