首页 > Web开发 > 详细

angularjs学习:通信

时间:2015-05-27 12:33:50      阅读:193      评论:0      收藏:0      [点我收藏+]

和服务器通信的方式:

1.$http:是简单的封装了浏览器原生的XMLHttpRequest对象。$http只能接受一个参数的函数,这个参数是一个对象,包含了用来生成HTTP请求的配置内容。这个函数返回一个promise对象,具有success和error两个方法。

$http({
method: ‘GET‘,
url: ‘/api/users.json‘
}).success(function(data,status,headers,config) {
// 当相应准备就绪时调用
}).error(function(data,status,headers,config) {
// 当响应以错误状态返回时调用
});
//方法实际上返回了一个promise对象 var promise = $http({
method: ‘GET‘,
url: ‘/api/users.json‘
});
// 或者使用success/error方法
promise.success(function(data, status, headers, config){
// 处理成功的响应
});
// 错误处理
promise.error(function(data, status, headers, config){
// 处理非成功的响应
}); 


2.$resource:可以理解为restful框架的一个资源创建工厂类。

//1.创建一个资源对象
var User = $resource(‘/api/users/:userId.json‘,
{userId: ‘@id‘}
});
//2. 发起一个restful格式的GET请求:
// GET /api/users/123
User.get({
id: ‘123‘
}, function(resp) {
// 处理响应成功
}, function(err) {
// 处理错误
});
// 发起restful格式的DELETE 请求:
// DELETE /api/users/123
User.delete({}, {
id: ‘123‘
}, function(response) {
// 处理成功的删除响应
}, function(response) {
// 处理非成功的删除响应
});
//等等restful格式请求








angularjs学习:通信

原文:http://my.oschina.net/haoqoo/blog/420451

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!