Help with XMLHttpRequest and json requests

Hello Ionic Community,
i’m having troubles with my project, i need to read from a server a simple json request, but i had this error : XMLHttpRequest cannot load http://localhost:8443/RESTfulExample/json/product/get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

I have tried to get the data from another page,just for test my controller (from this server https://public-api.wordpress.com/rest/v1/freshly-pressed/ ) and it work fine.

This is my code for instruct the controller:

 app.controller('controlloVista', function($scope , $http , $templateCache , vettoreCose ){

      $scope.listaDiCose = vettoreCose.getTutto();

  $scope.code = null;
  $scope.response = null;
  $scope.method = "get";
  $scope.url = "http://localhost:8443/RESTfulExample/json/product/get"//"http://127.0.0.1:8443/RESTfulExample/json/product/Alfredo";
  $scope.datiRicevuti = [];
  //$scope.url = "https://public-api.wordpress.com/rest/v1/freshly-pressed/";
  //$scope.url = "/json/file.json";
       
  $http({method: $scope.method, url: $scope.url, cache: $templateCache}).
          success(
            function(data, status) 
              {
                $scope.status = status;
                $scope.datiRicevuti = data;
              }).
          error(
            function(data, status) 
              {
                $scope.datiRicevuti = data || "Request failed";
                $scope.status = status;
              });

    

        
});
1 Like

Hi,

This is probably because the server side (!) is not handling the request correctly and not returning an ‘Access-Control-Allow-Origin’ header. You might want to check in your browser while running the app what is send to and received from the server, and configure the server accordingly. See also [this article][1] for additional info.

Regards,
Joost
[1]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Thanks…for se rapid response I will keep you updated…

victor

It worked? already I tried many ways and I can not get a json