目录

后端使用CrossOrigin页面还显示跨域问题

后端使用@CrossOrigin页面还显示跨域问题?

后端使用@CrossOrigin页面还显示跨域问题?

前后端分离项目,后端设置的允许跨域的代码,到前端调试界面的时候还显示了不允许跨域。

https://i-blog.csdnimg.cn/blog_migrate/b587beb4080877753867ea839da497d4.png

错误信息如下:

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.

https://i-blog.csdnimg.cn/blog_migrate/343ae311e38c7446c43bc65cca61a984.png

解决方案:

withCredentials: false

https://i-blog.csdnimg.cn/blog_migrate/80bc2847f148d5818da6c493796a5ab4.png

跨域问题解决:

https://i-blog.csdnimg.cn/blog_migrate/d0a1320dd4758f3acde37a18056e062a.png

原因:

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 源码:

https://i-blog.csdnimg.cn/blog_migrate/b9cdd1ec8e68b7ddcea4055108de47e1.png