My Application is not responding to the request given by front end and throwing a CORS exception

Bhavya :

I have created a web service using dropwizard.

  1. Through this, I am able to create resources, wherein I have created an entity for indexing the document, search request, getting doc by id.

  2. By hitting the API in postman I get the desired result.

  3. For developing the web application I built the front end using ember.js.

  4. But when I send a request to my application I am getting CORS policy exception.

    • When I am sending request through postman to http://localhost:9090 (9090 is the port number of my ApplicationConnector) then it is responding to the request.
    • Now when I send request from origin http://localhost:4200 (4200is the default port number for ember.js) to http://localhost:9090 it is giving error :

Access to XMLHttpRequest at http://localhost:9090/{indexName}/{id} >from origin http://localhost:4200 has been blocked by CORS policy: Response to preflight request doesn't pass access control check:
No 'Access->Control-Allow-Origin' header is present on the requested resource.

So can someone please explain how to resolve this issue and if there is any other way apart from enabling CORS in Dropwizard ?

jelhan :

You need to enable CORS in Dropwizard or serve both the frontend and the API from the same host (hostname and port). Otherwise it's forbidden by CORS security feature, which is enabled by default in all major browsers. Postman does not care about CORS.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133142&siteId=1