Joy*_*Joy 0 java jersey cors tomcat7 angular
我正在开发 Angular 9 应用程序,并使用 Angular httpclient 发送补丁要求。但由于预检请求未通过,要求总是失败。我知道我可以添加一些关于允许方法的设置,我做了,但仍然不起作用,似乎我的设置不适用于角度 httpclient。我在这个问题上纠结了很长时间,请大家给一些关于这个问题的建议。服务器是Tomcat 7(7.0.106),rest api是jersery,这里是web.xml中设置的一部分
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Authorization,Content-Type,X-Requested-With,Origin,Accept,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH</param-value>
</init-param>
</filter>
Run Code Online (Sandbox Code Playgroud)
这是 Angular httpclient 在 Chrome 中的请求详细信息(失败的)
**General**
Request URL: http://localhost:8080/api/v1/user/testPatch
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: [::1]:8080
Referrer Policy: strict-origin-when-cross-origin
**Request Header**
...
Accept: */*
Accept-Encoding: gzip, deflate, br
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: PATCH
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:8080
Origin: http://localhost:4200
Pragma: no-cache
Referer: http://localhost:4200/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
...
**Response Header**
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,Content-Type,X-Requested-With,Origin,Accept,Access-Control-Request-Method,Access-Control-Request-Headers
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS
Access-Control-Allow-Origin: http://localhost:4200
Allow: OPTIONS,PATCH
Content-Length: 803
Content-Type: application/vnd.sun.wadl+xml
Server: HTTP Server
...
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,PATCH方法没有在“Access-Control-Allow-Methods”中,但我已经在web.xml中设置了它,我不知道我错过了什么。顺便说一句,我可以通过 Postman 成功调用相同的 api,并且响应中的“Access-Control-Allow-Methods”设置与我在 web.xml 中设置的完全相同
我终于找到了问题,但首先感谢@Andrei 和@Paul Samsotha 的回复。这个问题的原因是我使用了Chrome cors插件,该插件会覆盖cors请求的allow方法,并且allow方法的默认值不包含“PATCH”。
| 归档时间: |
|
| 查看次数: |
3498 次 |
| 最近记录: |