在 WebSphere Liberty 上,我有一个在https://localhost:9443/context-root/objects上运行的后端。它可以工作,我可以从浏览器或邮递员测试我的 REST API。
我使用 Visual Studio Code 在 Angular 中编写了一个 Web UI。当我尝试使用 Crome localhost:4200 使用 REST API 的 UI http 调用测试('> ng serve')时,我收到错误:
没有 CORS 设置的 server.xml
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'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
响应标头
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:origin, content-type, accept, authorization
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS, HEAD
Access-Control-Allow-Origin:*
Access_Control_Max_Age:43200
Cache-Control:no-store, no-cache=set-cookie
Content-Length:284
Content-Type:application/json
带有 CORS 设置的 server.xml
<cors domain="/context-root" allowedOrigins="http://localhost:4200" allowedMethods="GET, DELETE, POST, PUT" allowedHeaders="accept" allowCredentials="true" maxAge="3600" />
错误:
Failed to load https://localhost:9443/context-root/objects: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:4200, *', but only one is allowed. Origin 'http://localhost:4200' is therefore not allowed access.
响应标头:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:origin, content-type, accept, authorization
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS, HEAD
Access-Control-Allow-Origin:http://localhost:4200
Access-Control-Allow-Origin:*
Access_Control_Max_Age:43200
我不确定为什么我在响应标头中得到双值,以及为什么特别是我得到 2 个允许来源。有没有解决的办法?谢谢