【发布时间】:2019-12-18 00:41:25
【问题描述】:
我的应用程序有角度前端和 springboot 后端。 当我从前端 (localhost:4200) 调用后端 api (localhost:8080/test) 时,它给出了一个错误。
Access to XMLHttpRequest at http://localhost:8080/api/XXX/1 from origin http://localhost:4200 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.
我的请求听众信息如下,
Request URL: http://localhost:8080/api/testSuite/execute/1
Referrer Policy: no-referrer-when-downgrade
Provisional headers are shown
Accept: application/json
Content-Type: application/json
Referer: http://localhost:4200/testSuite
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
【问题讨论】:
-
那么你是否在 Spring 中为前端启用了 CORS?
-
在后端启用 CORS,并且不要忘记允许 OPTIONS 请求,因为浏览器会在飞行前发送一个。
标签: angular spring-boot cors