How to get JSON data with Angular

0

I am studying angular and I am trying to receive JSON data from a webservice, but it has the following error:

  

XMLHttpRequest can not load    link . At the   'Access-Control-Allow-Origin' header is present on the requested   resource. Origin ' link ' is therefore not allowed access.

Follow my cod below.

app.controller("jornalCtrl", function($scope, $http) { 

    var url = 'http://localhost:8080/newspaper/dados/jornal';

    $http({
        method: 'JSONP',
        url: url,
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Origin': '*'
        },
        params:   {
            format: 'jsonp',
            callback: 'JSON_CALLBACK'
        }
    })
    .then(function successCallback(response) {
        $scope.jornais = response.jornais.jornal; 
    },
    function errorCallback(response) {
        $scope.content = "Something went wrong!";
    });
});
    
asked by anonymous 27.10.2016 / 20:03

2 answers

1

This is a security provided by browsers that do not allow you to access a server that has not allowed you, your host, dns, ip to consume it.

The webservice would have to enable localhost to access it, consume its services.

    
27.10.2016 / 20:07
1

You need to enable CORS (sorry, I did not find the same documentation in Portuguese ) in your service. Here you will need to search the service documentation that you are using to allow it, in Spring Boot for example you need to include the annotation:

@CrossOrigin(origins = "*")

Access control should actually be released on the server. A common mistake is to try to allow CORS on the client.

    
28.10.2016 / 19:37