0

I'm currently developing an Ionic App that fetches from an external API. Because this API is also developed by myself, everything is local. The API returns a JSON array.

My current controller looks like following:

app.controller('ListController', ['$scope', '$http', function($scope, $http){
       $http.get('http://localhost:8080/api/getPoi/merzouga').success(function(data){
       $scope.pois = data;
   });
}]);

The problem is that when I try to load this into my view I get the following error:

XMLHttpRequest cannot load http://localhost:8080/api/getPoi/merzouga. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

I don't know if it is relevant, but Ionic is being served on port 8100 and the API on 8080.

Any idea on how can I test this locally?

2 Answers 2

1

Install and enable CORS plugin in your browser.

Sign up to request clarification or add additional context in comments.

Comments

1

The browser is telling you how to solve the problem. Since you have the API in a different port, you need to send the "Access-Control-Allow-Origin" header along with your response (server-side). Supposing you are using Express, it'll be like this:

res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Origin', '*');

Or even better, you can just use a middleware for that: https://www.npmjs.com/package/cors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.