0

I am experimenting with some web programming. I wrote a python API that will make a query to an SQL database and return a JSON. The API works as expected.

On the other hand, I have a controller where I make the GET request to use the JSON.

The GET request is made and the API gets rolling; however, when the API returns the JSON the following appears in the developer console of the browser:

Failed to load http://localhost:3000/api/s1r2_rep_fis: No 'Access- Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:8000' is therefore not allowed access.

I guess this is due to permissions. My http server is run by python:

python -m http.server

The controller code is:

app.controller("test", function($scope, $http){
    $http.get("http://localhost:3000/api/s1r2_rep_fis")
    .then(function(response){
        $scope.Clientes = response.data;
    });
});

Please forgive me if I mix up some terms/technology, this is my first time using html/javascritp/angularJS.

Any help will be deeply appreciated

Vikas
  • 11,859
  • 7
  • 45
  • 69
Nerdrigo
  • 308
  • 1
  • 5
  • 14
  • has nothing to do with the backend call, you are making a call to a different domain – epascarello May 11 '18 at 01:15
  • @epascarello what do you mean to a different domain? – Nerdrigo May 11 '18 at 01:20
  • https://stackoverflow.com/questions/21854516/understanding-ajax-cors-and-security-considerations – epascarello May 11 '18 at 01:20
  • My guess is you are loading html page from one port and the python code in another – epascarello May 11 '18 at 01:21
  • @epascarello you are correct, the API is listetint con localhost:3000 and the server runsin localhost:8000. If I change the API port to 8000 I get an error saying that the Address is already in use. – Nerdrigo May 11 '18 at 01:33
  • Just to test your code you could disable the chrome's browser security and try. Or you can create a proxy to your API. Or host your client in the same domain where you host your API. – Senal May 11 '18 at 01:36

1 Answers1

0

From my understanding, this is a server-side issue. Not client side. All you need to do is to add CORS enable header on your server.

Access-Control-Allow-Origin: *

You can also check here how to enable CORS.

enable-cors.org

Kelvin
  • 32
  • 6
  • I think I get it now, I tried to use this: https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server, however id did not work – Nerdrigo May 11 '18 at 01:42