gpt4 book ai didi

java - 存在“Access-Control-Allow-Origin” header

转载 作者:太空宇宙 更新时间:2023-11-04 10:27:06 25 4
gpt4 key购买 nike

我有一个带有功能的简单 Controller 。

@RestController
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
@RequestMapping("/api")
public class DockerController {

@Autowired
private DockerService dockerService;

@PostMapping("/docker/container/{containerName}/pause")
public boolean pauseContainer(@PathVariable String containerName) {
dockerService.pauseContainer(containerName);
return true;
}

}

但是当我尝试从角度应用程序运行它时,我得到了

zone.js:2969 POST http://localhost:8080/api/docker/container/Env666/pause 403 () http://localhost:8080/api/docker/container/Env666/pause: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

这种情况在我使用服务功能后开始发生。

最佳答案

我成功重现了 CORS 错误,与Spring CrossOrigin Javadoc相反告诉你:

By default the supported methods are the same as the ones to which a controller method is mapped.

我必须在 CrossOrigin 注释中手动指定方法:

@CrossOrigin(origins="http://localhost:4200", allowedHeaders="*", methods={RequestMethod.POST})

关于java - 存在“Access-Control-Allow-Origin” header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50411907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com