Error calling REST with Angularjs

Asked

Viewed 347 times

0

I’m using the angular $http to make REST calls on my server, but I’m having a problem:

No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.

I’ve tried a lot of things, but nothing’s solved me yet, someone knows how to fix?

My Code:

.factory('CotacaoService', ['$http', function($http){
var cotacao = [];
$http({
  method: 'GET',
  url: 'http://192.168.254.8:8084/CotacaoREST/recursos/cotacoes/1/991'
}).then(function successCallback(response) {
    cotacao = response.data;
  }, function errorCallback(response) {
    console.log(response);
  });
return cotacao;

}]);

  • What are you using in the backend? Springrest, Jersey? What technology?

  • I’m wearing Jersey

  • I do not know deeply the Jersey. In the case of Spring the note org.springframework.web.bind.annotation.CrossOrigin resolve. By quickly searching I found something talking to include in the Response header the following attribute: "Access-Control-Allow-Origin", "*". See if this link help you.

  • You are testing in the browser or in an emulator?

  • josivan valeu, I will test with this example. Andre, I am testing in browser, Chrome Console or Firefox returns me this error

  • In the browser you have to give a command to enable. I’m doing a project also that only scrolls in the emulator or the device itself, in Chrome and firefox the same error. You have some tips on this link http://stackoverflow.com/questions/4819060/allow-google-chrome-to-use-xmlhttprequest-to-load-a-url-from-a-local-file

  • I did the tests here with the change in jersey and running this command on Chrome and still gives the same problem, I was even looking at a tutorial to add three parameters in the header, and also did not work, the strange thing is that only happens with the angle, I have an android project that consumes the webservice and works normal, someone has some idea?

Show 2 more comments

1 answer

0


Try creating a filter to work with CORS:

import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;

@Provider
public class CORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext request,
            ContainerResponseContext response) throws IOException {
        response.getHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHeaders().add("Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization");
        response.getHeaders().add("Access-Control-Allow-Credentials", "true");
        response.getHeaders().add("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS, HEAD");
    }
}

Source: https://stackoverflow.com/questions/28065963/how-to-handle-cors-using-jax-rs-with-jersey

Another solution would be that one.

  • I had done this way, but now I found the problem, this method solves on the server, but at the angle I had to take out the headers, thus solving my problem. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.