Rest angular Access-Control-Allow-Origin

1

I get this error:

  

Request from another blocked source: the same origin policy   prevents reading the remote resource in link   (reason: missing the CORS header 'Access-Control-Allow-Origin').

My server is an application Java in tomcat 8

In another project I have this:

var App = angular.module('SimulatorApp', ['ngRoute', 'ngResource']);
App.controller('EOGController', ['$scope','$resource',function($scope, $resource) { 
    $scope.loadData = function() {
        var Something = $resource("http://localhost:8090/test/databases/test", {id: "@id"});
        $scope.something = Something.get({id:15});                                          
    }   
}]);

Really if it calls, it gives me code 200 but the error that I mentioned before.

IF I call it from poster it works without problems or if in the browser I put the url : http://localhost:8090/test/databases/test?id=15 tb funciona

    
asked by sirdaiz 19.12.2016 в 13:04
source

3 answers

2

When from your AngularJS site you try to access your site on the server, it can respond that not all sites can access this data, Origin is a domain, all this by default is inaccessible from another source than is not the original.

Add to your server

<init-param>
    <param-name>cors.supportedHeaders</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Last-Modified</param-value>
</init-param>

And from the call of your AngularJS add contentType

contentType: "application/x-www-form-urlencoded;"

Asi:

$http({
     method: 'GET',
     url: 'http://localhost:8090/test/databases/test',
     params: {id: "@id"},
     contentType: "application/x-www-form-urlencoded"

}).then(function success(response) {

}, function error(response) {

});
    
answered by 19.12.2016 / 13:39
source
0

To make cross requests, the host must explicitly allow a request from the client referer. So you must add the following header:

Access-Control-Allow-Origin: http://sitio-del-request.com

where site-del-request.com is the site that executes the request.

By this I mean that the problem is not in Angular, but in the server in Java.

    
answered by 19.12.2016 в 13:29
0

@sioesi I have added the line both in the web.xml of my tomcat server and in the web.xml of my java web application

var Something = $resource("http://localhost:8090/test/databases/test", {id: "@id"});
$scope.something = Something.get({id:15});

Me sigue dando el mismo problema

I have tried with yours: App.controller ('EOGController', ['$ scope', '$ http', '$ resource', function ($ scope, $ http, $ resource) { ....     $ http ({              method: 'GET',              url: ' link ',              params: {id: "@id"},              dataType: "json",              contentType: "application / x-www-form-urlencoded"

    }).then(function success(response) {
        $scope.something = Something.get({id:15});  
    });

Error 400: Request from another blocked source: the same source policy prevents reading the remote resource in link (reason: missing the CORS header 'Access-Control-Allow-Origin').

    
answered by 19.12.2016 в 17:05