angualrjs 配置超时时间

timeout 1



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

https://docs.angularjs.org/api/ng/provider/$httpProvider#defaults

timeout 2



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

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

https://docs.angularjs.org/api/ng/service/$http#$http-returns

代码


(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‘);
      }
    ])

})();

原文地址:https://www.cnblogs.com/wancy86/p/8686056.html

时间: 2024-07-31 11:51:06

angualrjs 配置超时时间的相关文章

dubbo面试题之dubbo-client 和dubbo-server同时配置超时时间,是以哪个为准;

根据官网表示: 以 timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费方优先,提供方次之. 参考:http://dubbo.apache.org/zh-cn/docs/user/configuration/xml.html 原文地址:https://www.cnblogs.com/hujinshui/p/10459404.html

【Spring Cloud 源码解读】之 【如何配置好OpenFeign的各种超时时间!】

关于Feign的超时详解: 在Spring Cloud微服务架构中,大部分公司都是利用Open Feign进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务要进行比较繁杂的业务计算,那后台很有可能会出现Read Timeout这个异常. 1.关于hystrix的熔断超时 如果Feign开启了熔断,必须要重新设置熔断超时的时间,因为默认的熔断超时时间太短了,只有1秒,这容易导致业务服务的调用还没完成然后超时就被熔断了. 如何配置熔断超时: #Feign如

Mybatis设置超时时间

Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点中,增加如下配置<settings>  <setting name="defaultStatementTimeout" value="25"/>  </settings> 以秒为单位的全局sql超时时间设置,当超出了设置的超时时间时,

config文件中可以配置查询超时时间

web.config配置数据库连接 第一种:获取连接字符串 首先要定义命名空间 system.configuration 1.  string connstr= string constr = ConfigurationManager.AppSettings["connstring"]; web.config文件:加在<appsettings>和</appsettings> 之间 <appsettings> <add key="con

Confluence 6 配置数据库查询超时时间

如果数据库的查询时间太长同时你的应用程序显示没有响应,你可以配置数据库的查询超时时间.在默认情况下 Confluence 没有超时时间.希望配置数据库查询超时时间,在你的测试服务器上进行下面的操作: 1. 关闭 Confluence. 2. 从 confluence-x.x.x.jar 文件中解压出 databaseSubsystemContext.xml.这个文件在 confluence/WEB-INF/lib/,同时拷贝到 confluence/WEB-INF/classes/ 目录下面.

linux下ssh超时时间配置

摘自:https://www.cnblogs.com/flishroom/p/11661515.html Linux下设置超时时间,是在配置文件/etc/profile里.在该文件下,添加一个变量: export TMOUT=60 时间单位是S,上面配置的超时时间是60秒. 60秒无人操作ssh终端,则ssh自动退出. 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/12411251.html

Apache服务停止:信号灯超时时间已到,指定的网络名不再可用

环境说明:Apache2.4.10,Windows Server 2008 R2 问题说明: apache服务用于下载文件,但是在运行一段时间后,突然挂了. 其错误提示如下所示: [error] (730038)An operation was attempted on something that is not a socket.: winnt_accept: AcceptEx failed. Attempting to recover. [error] (OS 10038) : Child

windows 2008创建群集“xxx”时出错。由于超时时间已过,该操作返回

操作系统环境:windows 2008 Enterprise R2 SP1 验证群集配置时未发现任何的错误.但在创建故障转移群集时,出现超时错误的现象:"创建群集时出错.创建群集"xxx"时出错.由于超时时间已过,该操作返回.". 尝试单独选择一台服务器群集创建成功,但添加第二节点时,一直卡在"正在等待节点xxx 成为群集的完成起作用成员的通知",直到超时出错退出"由于超时时间已过,该操作返回." 排错过程: 使用单一节点创建

Tuxedo 超时时间控制(转贴)

以下是转贴: ----------------------------------------------------------------------------------------------------------------------------------------------- 源于才文章确实详细,暂且转载于此,谢原发帖主人. Tuxedo 超时控制(转贴)原帖发于DEV2DEV,现转贴在此. TUXEDO超时控制全功略 摘要: 本<功略>集中了TUXEDO应用中,可能涉