2013-10-23 83 views
0

我已经在java中编写了一个restful服务,并从angularjs调用它,但它不工作。如果我把静态json格式的作品。但不适用于RESTful服务。以下是我在服务文件中的代码。如何从angularjs调用restful服务

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' } 
    }) 
}); 

和我的控制器文件

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.'; 
}]); 

回答

1

有一个叫Restangular的AngularJS服务,您可以轻松地实现您的应用程序RESTful服务。

这可以简化常见的GET,DELETE和UPDATE请求,并且客户端代码最少。它非常适合任何从RESTful API使用数据的WebApp。

欲了解更多详细信息,请参阅Restangular

0

你注入ngResource并包括在你的代码角resource.min.js?这里是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']; 
+0

对不起的计算器代码的东西不喜欢我的贴,但看到小提琴。由于使用模拟帐户进行了多次访问,因此运行时出现错误,但似乎正常工作。 –