I have a simple project based on @RestController and AngularJS. I can send GET requests from Angular to @RestController but i could not send POST request. I have an error in
You need to add additional options in the Access-Control-Allow-Headers
Here are the options usually used:
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
You could also consider using the native CORS support available in the upcoming Spring Framework 4.2 release. See this blog post for more details.
By default, it is configured to automatically accept all the headers, so it should work as soon as you enable CORS support with CrossOrigin
annotation or with Java config.
In Spring 4.2 and further releases, there is a first class support for CORS out-of-the-box. The details can be found on this page.
In the Spring classes annotated with RESTController, @CrossOrigin annotation can be used. Following code represents the same:
@CrossOrigin(origins = "http://localhost:3000", maxAge = 3600)
@RestController
public class AngularController {
}
In the above code, localhost:3000 represents Angular app URL.