I am sending reactive form data through post request to java restful web api but it shows me CORS error While same is working fine in GET request. I already set the CORS header in response of rest api.
Angular Service Code :
private headers = new Headers({ 'Content-Type': 'application/json'});
baseURL='http://127.0.0.1:8080/userInformation/rest/UserService';
addData(formdata: any){
var body = JSON.stringify(formdata);
return this.http.post(this.baseURL+'/adduser',body)
.subscribe(
res => {
console.log(res);
},
err => {
console.log('Error occured');
}
);
}
JAVA Rest API CODE :
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/adduser")
public Response createUser(String val){
Gson gson = new Gson();
User user = gson.fromJson(val, User.class);
userDao.insertUser(user);
String result="SUCCESS";
return Response.ok().entity(result)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD")
.build();
}
Header Description : enter image description here I think when we are sending the data to rest api we need to set the CORS origin header to our angular code. How can I do this?