Error with Angularjs for $http.get

Asked

Viewed 215 times

1

I’m trying to fetch some data via WS(Web Service) only when I ask to return in my app gives this error:

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

But when I try to search via console it returns normally.

var App = angular.module('App', ['ionic']);

//listar clientes via ws somente ativos

App.get('ListaCtrl', function($scope, $http){
   $http.post('http:// meu url da ws').
       success(function (data) {
           $scope.ret = data;
   });
});

1 answer

2


When you are consuming data from other domains, it is necessary that external domain access is released in WS in order for it to work. By default, browsers and servers block access that is not from the same domain to prevent intrusions.

Another method that can be seen is to make the request sent in JSONP format, which in most Webservices is already released as default... In the case of Angularjs used above (note by $http.get) you can do something like the example below:

$http.get({
  url : '/api/contest/submit.action', 
  data : { params: $param, responseType: 'jsonp' }
});

NOTE Here’s a solution, I haven’t tested it yet but it should probably work...

Enable CORS with Angularjs

Browser other questions tagged

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