Frontend: Angular5
Backend: Java (Running on a Wildfly 8.x server)
On making an HTTP POST
request with content-type: 'application/json'
from my Angular application to the server, I get the following error:
Failed to load http://localhost:8080/myk/api/v1/events/addevent: Response to preflight request doesn't pass access control check: 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 is the code for the request:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
this.http.post("http://localhost:8080/myk/api/v1/events/addevent",
jsonPayload,
httpOptions).subscribe();
On inspecting, I found that instead of a POST
request, an OPTIONS
request was being made (as in accordance with this MDN article on CORS and preflighted requests).
However, when I tried the same request using Postman, it worked perfectly fine.
How does Postman not face an issue with the OPTIONS
request when I'm not handling the OPTIONS
request at the server?
Also, what can I do (while maintaining the same content-type
and without having to modify the server side code) to solve this issue?
Note 1: Although this question addresses the same issue, the suggested solution requires changing the content-type.
Note 2: When I run another application (designed, not by me, in Angularjs) which attempts to make a POST request using an ajax
call, it works fine. (I do not understand why this works and, besides, an answer related to angular5 would be appreciated. I included this note in case it helps in figuring out the problem with the Angular5 application)
EDIT: This might be bordering on fussy, but I'm specifically looking to try to solve the above by modifying client side code and not by using extensions or any modifications related to the browser. However, if none of that is possible, I'm open to changing server-side code.