sasi kanth
sasi kanth

Reputation: 2927

How to call restful services from angularjs

I've written a restful service in java and calling it from angularjs but it's not working. If I put static json format its works. But is not working for RESTful services. Below is my code in service file.

angular.module('intelesant.services', [])
    .value('version', '0.1')
    .service('customers1',function(){
         return $resource('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo', {}, {
             query: { method: 'GET', isArray: true },
             create: { method: 'POST' }
     })
});

and my controller file is

intelesant.controller('HomeController', ['$scope','customers1', function(s,customers) {

    alert(JSON.stringify(customers));
    s.homeContent = 'Test Content for Home page. This can come from server via REST service.';
}]);

Upvotes: 0

Views: 2148

Answers (2)

kds
kds

Reputation: 28665

There is an AngularJS service called Restangular which you can easily implement RESTful Services in your Application.

This simplifies common GET, DELETE, and UPDATE requests with a minimum of client code. It's a perfect fit for any WebApp that consumes data from a RESTful API.

For more details please refer Restangular

Upvotes: 1

Dan Doyon
Dan Doyon

Reputation: 6720

Are you injecting ngResource and including angular-resource.min.js in your code? Here is fiddle accessing your service

angular.module('myApp', ['ngResource']);
function Ctrl($scope,$resource) {
    var Geonames = $resource('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo', 
        {   }, 
        {
          query: { method: 'GET', isArray: true },
          create: { method: 'POST' }
        }
     );
     $scope.objs = Geonames.query();
   };
};
Ctrl.$inject = ['$scope','$resource'];

Upvotes: 0

Related Questions