angular学习笔记(二十三)-$http(1)-api
之前说到的$http.get和$http.post,都是基于$http的快捷方式.下面来说说完整的$http:
$http(config)
$http接受一个json格式的参数config:
config的格式如下:
{
method:字符串 ,
url:字符串,
params:json对象,
data:请求数据,
headers:请求头,
transformRequest:函数,转换post请求的数据的格式,
transformResponse:函数,转换响应到的数据的格式,
cache:布尔值,
timeout:数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错误
withCredentials:布尔值
}
1.method: (必填)
请求的方法,字符串格式,'post','get','head','delete','put','jsonp'
(其中head方法和get请求方式一致,但是head方法没有响应体,只有响应状态码,用于检测该url是否可以获得get请求)
2.url: (必填)
请求的路径,字符串格式
3.params:
请求带有的参数,json对象:
{key1:'value1',key2:'value2'}
//转换成
url?key1=value1&key2=value2
4.data:
post请求所提交的数据,字符串或者json对象
5.headers:
请求头,json对象,可以自定义配置http请求头的参数
6.transformRequest:
函数,转换post请求的数据的格式
7.transformResponse:
函数,转换响应到的数据的格式
8.cache:
布尔值,是否启用缓存(暂时不懂,需要请教老公)
9.timeout:
数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错误
10.withCredentials:
不懂
$http()返回一个对象,该对象有三个方法:
1. then([success],[error]):
.then()方法传入两个参数:
[success]: function(res){}
第一个参数是一个请求成功的回调函数,该函数接受1个参数res,res是一个json对象,它有四个属性值,分别是data,status,headers,congfig,这四个值就是http://www.cnblogs.com/liulangmao/p/3864954.html这里面的.success()方法传入的函数的四个参数
[error]: function(res){}
第二个参数是一个请求失败的回调函数,该函数接受1个参数res,res是一个json对象,它有四个属性值,分别是data,status,headers,congfig,这四个值就是http://www.cnblogs.com/liulangmao/p/3864954.html这里面的.error()方法传入的函数的四个参数
2. success(function(){})
.success方法传入一个参数,是请求成功的回调.该函数接受4个参数,分别就是data,status,headers,congfig,和http://www.cnblogs.com/liulangmao/p/3864954.html里面的.success()方法传入的函数的四个参数是一样的.
3. error(function(){})
.error方法传入一个参数,是请求失败的回调.该函数接受4个参数,分别就是data,status,headers,congfig,和http://www.cnblogs.com/liulangmao/p/3864954.html里面的.error()方法传入的函数的四个参数是一样的.
注意: 其中,.success和.error两个方法都返回调用自己的这个对象,所以这个对象还是具有了.then(),.success(),.error()方法,所以可以链式调用:
var obj1 = $http({
method:'GET',
url:'/name'
});
var obj2 = obj1.success(function(res){data,status,headers,config});
var obj3 = obj2.error(function(res){data,status,headers,config});
console.log(obj1 === obj2); //true
console.log(obj1 === obj3); //true
但是.then方法不是返回调用.then的对象... 它返回的是一个promise对象... 所以它只有.then方法,二不再有.success和.error方法:
var obj = $http({
method:'GET',
url:'/name'
});
obj.then(function(res){return res.data+'2'},function(res){return '失败'}).then(function(data){console.log(data)},function(data){alert(data)});
关于promise的.then()方法,详见: http://www.cnblogs.com/liulangmao/p/3907571.html
在这里无论请求是成功还是失败,第二次调用.then都会调用成功回调(因为它对promise发送的通知是成功通知),回调函数中的data就是上一个.then()的回调的返回值,如果是成功回调.也就是res.data+'2',如果是失败回调,那就是'失败'
angular学习笔记(二十三)-$http(1)-api的更多相关文章
- angular学习笔记(二十九)-$q服务
angular中的$q是用来处理异步的(主要当然是http交互啦~). $q采用的是promise式的异步编程.什么是promise异步编程呢? 异步编程最重要的核心就是回调,因为有回调函数,所以才构 ...
- angular学习笔记(二十八-附2)-$http,$resource中的promise对象
下面这种promise的用法,我从第一篇$http笔记到$resource笔记中,一直都有用到: HttpREST.factory('cardResource',function($resource) ...
- angular学习笔记(二十七)-$http(5)-使用$http构建RESTful架构
在angular中有一个特别为RESTful架构而定制的服务,是在$http的基础上进行了封装. 但是为了学习,我们先看看用直接$http是如何构建RESTful架构的: 假设有一个银行卡的列表.需要 ...
- angular学习笔记(二十六)-$http(4)-设置请求超时
本篇主要讲解$http(config)的config中的timeout项: $http({ timeout: number }) 数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错 ...
- angular学习笔记(二十五)-$http(3)-转换请求和响应格式
本篇主要讲解$http(config)的config中的tranformRequest项和transformResponse项 1. transformRequest: $http({ transfo ...
- angular学习笔记(二十四)-$http(2)-设置http请求头
1. angular默认的请求头: 其中,Accept 和 X-Requested-With是$http自带的默认配置 Accept:application/json,text/plain ...
- angular学习笔记(二十二)-$http.post
基本语法: $http.post('url',{},{}).success(function(data,status,headers,config){ }).error(function(data,s ...
- angular学习笔记(二十一)-$http.get
基本语法: $http.get('url',{}).success(function(data,status,headers,config){}).error(function(data,status ...
- angular学习笔记(二)-创建angular模块
如果在页面的html标签(或任意标签)中添加ng-app,表示对整个页面应用angular来管理. 他是一个模块. 模块有助于把东西从全局命名空间中隔离. 今天学习如何自定义创建模块: <!DO ...
随机推荐
- Java从零开始学二十五(枚举定义和简单使用)
一.枚举 枚举是指由一组固定的常量组成的类型,表示特定的数据集合,只是在这个数据集合定义时,所有可能的值都是已知的. 枚举常量的名称建议大写. 枚举常量就是枚举的静态字段,枚举常量之间使用逗号隔开. ...
- The jQuery HTML5 Audio / Video Library (jQuery jPlayer插件给你的站点增加视频和音频功能)
http://jplayer.org/ The jQuery HTML5 Audio / Video Library jPlayer is the completely free and open s ...
- 〖Linux〗clang3.4的编译与安装
1. 编译与安装clang3.4 sudo apt-get install -y g++ subversion cmake cd ~ mkdir Clang && cd Clang s ...
- Redis学习(4)-数据类型,string,hash
Redis数据类型: redis使用键值对保存数据 key:全部是字符串 value:五种数据类型:string,hash,List,Set,有序的Set集合. key命名:自定义,名字不要过长,否则 ...
- Linux下umask的缺省默认权限
Linux有缺省默认文件.文件夹权限umask.默认 777 -xxx(文件夹) 666 - xxx(文件) 11.查看当前用户umask R(4)--W(2)--X(1) [root@mvpban ...
- 【mysql】Innodb三大特性之adaptive hash index
1.Adaptive Hash Indexes 定义 If a table fits almost entirely in main memory, the fastest way to perfor ...
- How to forcefully delete a daemonset or a pod in kubernetes cluster
I have setup a kubernetes cluster which is working fine. I created deployment with type as daemonset ...
- Web前端开发笔试&面试_01(mi:)
—— (al_me16041719002000) begin—— 1.(单选)下面哪个方法是String对象和Array对象都有的? A.splice B.split C.replace D.conc ...
- JavaScript 中定义变量时有无var声明的区别
关于JavaScript中定义变量时有无var声明的区别 var a=5; //正确 a=5; //正确 在javascript中,以上两种方法都是定义变量的正确方法.微软的Script56.CHM中 ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...