后端有一个 Spring Boot 应用程序在运行。我已经实现了几个 GET/POST/PUT/DELETE API。
前端(AngularJS)可以做 GET 和 POST,但是当请求 PUT 和 DELETE 时会得到 403 ERROR。 我添加了允许的来源(POST 没有它就无法工作)来解决 CORS 问题。
CORS 设置:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("https://xxx1.com", "https://xxx2.com");
}
};
}
CSRF 设置:
http.csrf().disable();
暂时关闭
下面是我对 DELETE 的实现:
@DeleteMapping("/listings/{id}")
public ResponseEntity<Void> deleteListings(@PathVariable Long id) {
log.debug("REST request to delete listings : {}", id);
this.listingsService.delete(id);
return ResponseEntity.status(HttpStatus.OK).body(null);
}
有人可以帮忙吗?非常感谢!
最佳答案
看起来您可以链接 allowedMethods("GET","POST","PUT","DELETE")
according to the docs .
这有帮助吗?
关于angularjs - PUT/DELETE 出现 403 错误,但 GET/POST 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47720459/