简体   繁体   中英

'Access-Control-Allow-Origin' header is present

I have a simple Controller with a function.

@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;
}

}

But when i try to run it from the angular app i get

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.

This started happening after I used the service functionallity.

I successfully reproduce the CORS error, contrary to Spring CrossOrigin Javadoc telling that :

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

I had to specify the method manually in the CrossOrigin annotation :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM