javascript – 取消/中止angularJs中的所有待处理请求

前端之家收集整理的这篇文章主要介绍了javascript – 取消/中止angularJs中的所有待处理请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在路由更改时,我需要中止来自先前路由的所有待处理请求,以便我不会遇到来自先前路由的响应问题,从而弄乱我当前路由上的数据(有时在前一路由的响应需要很长时间才能完成时) .

我考虑过使用http interceptor

$httpProvider.interceptors.push(function($q) {
    return {
     'request': function(config) {

      },'response': function(response) {

      }
    };
  });

在请求函数中,我可以使用建议here的promise修改config.timeout,并将所有延迟对象存储在全局缓存中,以便我可以取消所有这些.

这种方法的问题在于它可能会覆盖代码中其他位置设置的config.timeout.

我认为另一种解决方案可能是取消XMLHttpRequest级别的所有ajax请求,但我不知道该怎么做.

有什么建议?谢谢.

解决方法

正如你所说,timeout是我们现在唯一使用的取消正在运行的$http请求的API.我认为你的拦截加上取消承诺你的钱是正确的.

您可以做的是在$http请求上附加完整的延迟对象,并取消路由更改处理程序中的所有pendingRequests.

像这样的东西可能(或许*)有效吗?

angular.module('module').config(function ($httpProvider) {
  $httpProvider.interceptors.push(function ($q) {
    return {
      request: function (config) {
        if (!config.timeout) {
          config.cancel  = $q.defer();
          config.timeout = config.cancel.promise;            
        }

        return config;
      }
    }
  });
});

angular.module('module').run(function ($rootScope,$http) {
  $rootScope.$on('$stateChangeStart',function () {
    $http.pendingRequests.forEach(function (pendingReq) {
      if (pendingReq.cancel) {
        pendingReq.cancel.resolve('Cancel!');
      }
    });
  });
});

*:我或许也许说,因为我采用这种方法取得了成功,但很少能找到像这样的银弹.

编辑

如果您需要绕过已取消的promise的错误处理程序,请挂钩到responseError属性并在那里进行手动操作.

angular.module('module').config(function ($httpProvider) {
  $httpProvider.interceptors.push(function ($q) {
    return {
      responseError: function (response) {
        if (response.config.timeout.$$state.value === 'Cancel!') {
          // debugger; 
          return $q.when('bypassed');
        }
      }
    }
  });
});

我开始认为没有通用/“酷”的解决方案来达到你想要的最终结果.在这里走一些奇怪的地方:)

EDIT2:

现在自己测试一下.在responseError中返回$q.when(‘something’)将有效地绕过已取消的$http请求的错误回调.如果它适合您,请告诉我.

原文链接:https://www.f2er.com/js/157470.html

猜你在找的JavaScript相关文章