Question

I have created an interface using express and mongoose to fetch data from mongodb and to send it to client side.It s working fine when i fetch the data using wget on linux terminal and on html browsers. But when I request the data using angular js $http method , it shows me the following errors.

   No 'Access-Control-Allow-Origin' header is present on the requested resource.         Origin 'http://127.0.0.1:9000' is therefore not allowed access.

Can anyone suggest me the error ofr how to get rid of this problem. Thanx in advance.

Was it helpful?

Solution

You are trying to do an AJAX ($http.get) call from a different host/domain/IP. My guess is that you're running your Angular app on http://localhost or http://127.0.0.1 and you're running Node on http://127.0.0.1:9000, which is a different Origin. There are two solutions:

  1. Run them on the same hostname, for example both on localhost or both 127.0.0.1.
  2. Add an Access-Control-Allow-Origin HTTP header from Node, allowing your Angular host (origin) to access the back end via AJAX.

The reason for all this is a security measure, so that websites cannot just fetch any other website on the internet without explicit permission. Otherwise malicious websites could do an AJAX call to your PayPal or bank account for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top