springboot No 'Access-Control-Allow-Origin' header is present on the requested resource
springboot“No ‘Access-Control-Allow-Origin’ header is present on the requested resource” is an error that occurs when a web browser is trying to access a resource from a different domain than the one that served the original page. This error occurs because of the same-origin policy, which is a security feature implemented in web browsers that blocks web pages from making requests to a different domain than the one that served the original page.
To fix this error, you will need to add the “Access-Control-Allow-Origin” header to the resource that you are trying to access. This can be done in a few different ways, depending on your setup:
- If you have control over the server that is serving the resource, you can add the “Access-Control-Allow-Origin” header to the HTTP response. For example, in Spring Boot, you can do this by adding the following code to your controller:
@CrossOrigin(origins = "http://example.com")
@GetMapping("/resource")
public ResponseEntity<Object> getResource() {
// code to retrieve and return resource
}
- If you do not have control over the server that is serving the resource, you can use a browser extension or plugin to add the “Access-Control-Allow-Origin” header to the HTTP request. There are several extensions and plugins available for different web browsers that allow you to do this.
Other Article on Tag springboot
- - Can I compile and run a spring boot jar from inside another spring boot application
- - Response to preflight request doesnt pass access control check It does not have HTTP ok status.
- - springboot ComponentScan multiple excludeFilters example
- - springboot CORS policy
- - springboot No 'Access-Control-Allow-Origin' header is present on the requested resource