如何访问的RESTful API的服务,我angularjs页? [英] How to access the services from RESTful API in my angularjs page?
问题描述
我很新的angularJS。我正在寻找从REST风格的API访问服务,但我没有得到任何的想法。怎么做到的?
I am very new to angularJS. I am searching for accessing services from RESTful API, but I didn't get any idea. How do that?
推荐答案
AngularJS提供 $ HTTP
服务了正是你想要做什么:发送Ajax请求的Web服务,并从他们那里接收数据,使用JSON(这是完全的交谈REST服务)
Option 1: $http service
AngularJS provides the $http
service that does exactly what you want: Sending AJAX requests to web services and receiving data from them, using JSON (which is perfectly for talking to REST services).
要举个例子(从AngularJS文档,并且稍微适应):
To give an example (taken from the AngularJS documentation and slightly adapted):
$http({ method: 'GET', url: '/foo' }).
success(function (data, status, headers, config) {
// ...
}).
error(function (data, status, headers, config) {
// ...
});
选项2:$资源服务
请注意,也有在AngularJS其他的服务, $资源
服务它提供了更高级时装访问REST服务(例如再次从AngularJS文档拍摄):
Option 2: $resource service
Please note that there is also another service in AngularJS, the $resource
service which provides access to REST services in a more high-level fashion (example again taken from AngularJS documentation):
var Users = $resource('/user/:userId', { userId: '@id' });
var user = Users.get({ userId: 123 }, function () {
user.abc = true;
user.$save();
});
选项3:Restangular
此外,也有第三方的解决方案,如 Restangular 。请参阅如何使用它的文档。基本上,它的方式更具声明并提取更多的细节远离你。
Option 3: Restangular
Moreover, there are also third-party solutions, such as Restangular. See its documentation on how to use it. Basically, it's way more declarative and abstracts more of the details away from you.
这篇关于如何访问的RESTful API的服务,我angularjs页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!