【发布时间】:2019-07-05 15:15:14
【问题描述】:
我正在尝试使用“put”动词使用 http 协议进行一次更新,但我使用 Request Method: OPTIONS from chrome 得到 403 禁止。
如果我尝试将动词从 PUT 更改为 POST,它会起作用。
这是我的服务器代码(spring)
@PutMapping("/path")
public ResponseEntity putMethod(@RequestBody Dto dto) throws URISyntaxException {
log.debug("put is called");
return ResponseEntity.ok().build();
}
这是我的角度代码
update(dto: dto) {
return this.http
.put<Dto>(this.resourceUrl + '/path', dto, { observe: 'response' })
.pipe();
}
如果我将 put 更改为 angular 并且 spring 日志写入正确,但我需要更新并且我想使用正确的动词
Chrome 日志
Request URL: http://localhost:9080/path
Request Method: OPTIONS
Status Code: 403 Forbidden
Remote Address: 127.0.0.1:9080
Referrer Policy: no-referrer-when-downgrade
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Connection: Close
Content-Language: it-IT
Content-Length: 20
Date: Fri, 05 Jul 2019 15:19:14 GMT
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
X-Powered-By: Servlet/3.1
Provisional headers are shown
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: PUT
Origin: http://localhost:4200
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
这里是我的java post映射
@PostMapping("/path")
public ResponseEntity postMethod(@RequestBody Dto dto) throws URISyntaxException {
log.debug("post is called");
return ResponseEntity.ok().build();
}
和角度邮政编码
post(dto: dto) {
return this.http
.post<Dto>(this.resourceUrl + '/path', dto, { observe: 'response' })
.pipe();
}
此处为发布请求(工作)的 chrome 日志
Request URL: http://localhost:9080/path
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:9080
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Origin: *
Content-Language: it-IT
Content-Length: 16
Content-Type: application/json;charset=UTF-8
Date: Fri, 05 Jul 2019 15:48:01 GMT
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
X-Powered-By: Servlet/3.1
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/json
Origin: http://localhost:4200
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
【问题讨论】:
-
您是否在任何阻止
put的代理背后? -
@PasupathiRajamanickam 不,我没有设置代理
-
发布此 ajax 调用的 chrome 错误日志。
-
更新日志问题。
标签: angular spring controller put