angular学习笔记(二十六)-$http(4)-设置请求超时
本篇主要讲解$http(config)的config中的timeout项:
$http({ timeout: number })
数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错误
demo:
html:
<!DOCTYPE html>
<html ng-app = 'HttpGet'>
<head>
<title>18.4 $http(2)</title>
<meta charset="utf-8">
<script src="angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller = "dataController">
<span>{{data}}</span>
</div>
</body>
</html>
js:
var jsonData = {name:"code_bunny"}; var httpGet = angular.module('HttpGet',[]); httpGet.factory('getData',function($http,$q){
return function(){
var defer = $q.defer();
$http({
method:'post',
url:'/api/user',
data: jsonData,
headers: {'Authorization':'code_bunny'},
timeout: 1000
}).success(function(data,status,headers,config){
defer.resolve(data);
}).error(function(data,status,headers,config){
defer.reject(data)
});
return defer.promise
}
});
httpGet.controller('dataController',function($scope,getData){
$scope.data = getData()
});
nodejs:
var express = require('express');
var bodyParser = require('body-parser');
var app = express(); // parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false })); // parse application/json
app.use(bodyParser.json()); app.use(express.static(__dirname+'')); var data = "name=code_bunny&age=3"; app.post('/api/user',function(req,res){
console.log(req.body);
setTimeout(function(){
res.send(data)
},2000)
}); app.listen(3000);
如代码所示,我们在后台设置2000毫秒以后返回内容,但是在$http的congif里设置等待超时为1000毫秒,所以,还不等到后台返回数据,请求就会被取消:
*注意: timeout只能在某个单独的$http()里面配置,不能通过$httpProvider.defaults.timeout进行配置
完整代码路径:https://github.com/OOP-Code-Bunny/angular/tree/master/OREILLY/18.4%20%24http(2)
angular学习笔记(二十六)-$http(4)-设置请求超时的更多相关文章
- 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学习笔记(二十八-附2)-$http,$resource中的promise对象
下面这种promise的用法,我从第一篇$http笔记到$resource笔记中,一直都有用到: HttpREST.factory('cardResource',function($resource) ...
- angular学习笔记(二十九)-$q服务
angular中的$q是用来处理异步的(主要当然是http交互啦~). $q采用的是promise式的异步编程.什么是promise异步编程呢? 异步编程最重要的核心就是回调,因为有回调函数,所以才构 ...
- angular学习笔记(二十八-附1)-$resource中的资源的方法
通过$resource获取到的资源,或者是通过$resource实例化的资源,资源本身就拥有了一些方法,$save,$delete,$remove,可以直接调用来保存该资源: 比如有一个$resour ...
- angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构
ngResource模块是angular专门为RESTful架构而设计的一个模块,它提供了'$resource'模块,$resource模块是基于$http的一个封装.下面来看看它的详细用法 1.引入 ...
- angular学习笔记(二十二)-$http.post
基本语法: $http.post('url',{},{}).success(function(data,status,headers,config){ }).error(function(data,s ...
- angular学习笔记(二十)-表单验证
本篇主要介绍angular中的表单验证: 表单验证主要有以下一些内容: 1. required指令: 相当于html5的required属性,验证不能为空 2. ng-maxlength属性: 验证内 ...
- angular学习笔记(三十)-指令(2)-restrice,replace,template
本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...
随机推荐
- flume hdfs一些简单配置记忆
############################################ # producer config ##################################### ...
- Emmet初探2
关于Emmet Emmet插件的前身是Zen coding,可以大幅度提高前端开发效率的一个工具,也有人说类似于jade(高性能的模板引擎,它深受 Haml 影响,它是用 JavaScript 实现的 ...
- 1z0-052 q209_7
7: In which of the scenarios will the DBA perform recovery? (Choose all that apply.) A.The alert log ...
- js 暂时性死区
1.概念 在代码块内,使用let.const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 TDZ). 2.注意 “暂时性死区”也意味 ...
- JavaScript 纯粹对象
JavaScript 纯粹对象 1.定义: 通过 "{}" 或者 "new Object" 创建的对象,像new Date(),new String()都不是纯 ...
- 关于linux的添加永久静态路由的static-routes方法
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...
- 使用spring的@Async异步执行方法
应用场景: 1.某些耗时较长的而用户不需要等待该方法的处理结果 2.某些耗时较长的方法,后面的程序不需要用到这个方法的处理结果时 在spring的配置文件中加入对异步执行的支持 <beans x ...
- js 展开/收起效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【laravel5.4】自定义404、503等页面
1.处理自定义错误或不存在页面:生产环境一定要关闭debug模式. public function render($request, Exception $exception) { if ($exce ...
- PHP中一些有用的函数
<?php /** * 加密解密 * * @param string $key * @param string $string * @param string $decrypt * @retur ...