博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angualrjs 配置超时时间
阅读量:7115 次
发布时间:2019-06-28

本文共 2220 字,大约阅读时间需要 7 分钟。

timeout 1


本想通过$httpProvider的defaults属性配置timeout时间, defaults中没有这个属性.

timeout 2


在拦截器中为request的config配置timeout, 在response中根据xhrStatus=timeout来处理超时. 奈何response中没找到这个xhrStatus

213419-20180401112302128-243653749.png

官网里说status=-1表示请求被取消, timeout时就会取消请求, 那就用这个来判断吧

213419-20180401112622577-930828619.png
213419-20180401112828779-1402036288.png

代码


(function() {  angular.module("mobile")    .factory('HttpInterceptor', ['$q',      function($q) {        return {          request: function(config) {            // 设置超时时间, 毫秒            config.timeout = 1;            if (config.url.search("service/") < 0)              return config;            if (localStorage.getItem('token')) {              config.params = config.params || {};              config.params['token'] = localStorage.getItem('token');              config.params['t'] = new Date().getTime();            }            return config;          },          requestError: function(err) {            return $q.reject(err);          },          response: function(res) {            console.log('res: ',res);            if(res.statusText=='timeout')            {              console.log('timeout: ',111);            }            if (res.data && res.data.code) {              switch (res.data.code) {                case '000':                  return res                case '100':                  // alert(res.data.msg)                  // 账号未注册                  localStorage.removeItem('token');                  localStorage.removeItem('groupid');                  return res                case '200':                  // session 无效                  console.log(res.data.msg)                  localStorage.removeItem('token');                  localStorage.removeItem('groupid');                  window.location.href = '';                  break              }            }            return res          },          responseError: function(err) {            if (-1 === err.status) {              // 请求超时              alert('请求超时');            } else if (500 === err.status) {              // 处理各类自定义错误            } else if (501 === err.status) {              // ...            }            return $q.reject(err);          }        };      }    ])    .config(['$httpProvider',      function($httpProvider) {        $httpProvider.interceptors.push('HttpInterceptor');      }    ])})();

转载地址:http://srghl.baihongyu.com/

你可能感兴趣的文章
C++对象的数据成员
查看>>
hashMap_使用
查看>>
iniparser解析文件说明。
查看>>
Apache2.4 多端口虚拟目录配置
查看>>
Node.js入门
查看>>
[百度之星]资格赛:IP聚合
查看>>
POJ 1328 Radar Installation 贪心
查看>>
python常用问题收集
查看>>
奇怪的sqlconnection.open错误
查看>>
【转】BehaviorDesigner学习
查看>>
【转】PHP date("Y-m-d H:i:s");获取当前时间 差8小时解决办法
查看>>
bzoj 1791 DP
查看>>
UIButton 之 按下高亮
查看>>
创建关系1
查看>>
poj3292(筛法+打表)
查看>>
HQL
查看>>
Modo
查看>>
XX公司在线笔试题编程题之一
查看>>
POJ NOI0105-40 数1的个数
查看>>
数(Number)
查看>>