Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs破坏http长池post请求_Angularjs_Http - Fatal编程技术网

angularjs破坏http长池post请求

angularjs破坏http长池post请求,angularjs,http,Angularjs,Http,我正在执行http post长池请求。我想在停止服务时取消该请求。 我的代码如下所示: app.service("ContactsService", function($http, $rootScope, $timeout, $q, ContactsFactory) { var requestCanceler = $q.defer(); this.startHttpRequest = function(userId) { var httpRequest = $http.po

我正在执行http post长池请求。我想在停止服务时取消该请求。 我的代码如下所示:

app.service("ContactsService", function($http, $rootScope, $timeout, $q, ContactsFactory) {
  var requestCanceler = $q.defer();

  this.startHttpRequest = function(userId) {
      var httpRequest = $http.post($rootScope.base_url + '/contacts/req', { toId: userId }, timeout: requestCanceler.promise});

 httpRequest.success(function(data){
      console.log("success");
    });
    httpRequest.error(function(data, status){
      console.log("error");
    });
      }

  this.stopService = function(){
    console.log("stop contacts service");
    requestCanceler.resolve("request canceled");
  } 

}
当我停止服务时,我的应用程序挂起,这是因为我执行requestCanceler.resolve;当我将其更改为.reject时,它没有挂起,但http post请求仍然挂起

我怎样才能打破post请求


谢谢。

AngularJS文档声明,AngularJS取消请求的承诺必须得到解决

超时–{number | Promise}–以毫秒为单位的超时,或在解析时应中止请求的承诺

这也可以在中看到,未设置错误/拒绝回调:

timeout.then(timeoutRequest);

不支持对被拒绝的承诺取消请求。

尝试向http承诺添加错误处理程序。很抱歉,我有成功和错误回调,如已编辑,但在解决propmise时我没有控制台日志…尝试链接处理程序和承诺请详细说明“我的应用程序正在挂起”!解决超时承诺是取消
$http
呼叫的正确方法。挂起意味着挂起:)我正在chrome的ripple中测试我的应用程序,所以我必须关闭该选项卡。它只是挂起了…谢谢大家,然后加上成功和错误,calbacks成功了:)