Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.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
Javascript 如何用$http区分已取消的请求_Javascript_Angularjs_Angular Http - Fatal编程技术网

Javascript 如何用$http区分已取消的请求

Javascript 如何用$http区分已取消的请求,javascript,angularjs,angular-http,Javascript,Angularjs,Angular Http,在$http错误处理程序中,我需要区分真正的http错误和已取消的请求 以下是一个例子: app.controller('Ctrl', function ($http, $q) { const vm = this; this.errors = []; vm.errorHandler = (label) => ((error) => { this.errors.push(label + ' : \n\n' + JSON.stringify(error, nul

$http
错误处理程序中,我需要区分真正的http错误和已取消的请求

以下是一个例子:

app.controller('Ctrl', function ($http, $q) {
    const vm = this;
  this.errors = [];

  vm.errorHandler = (label) => ((error) => {
    this.errors.push(label + ' : \n\n' + JSON.stringify(error, null, 2));
  });


  vm.unResolved = () => {
    $http({
      method: 'GET',
      url : 'https://Idonotexist'
    }).catch(vm.errorHandler('unResolved'));
  }

  vm.cancelled = () => {
  const canceller = $q.defer()
    $http({
      method: 'GET',
      timeout: canceller.promise,
      url : 'https://www.googleapis.com/books/v1/volumes?q=isbn:0747532699'
    }).catch(vm.errorHandler('cancelled'));
    canceller.resolve();
  }

  vm.unsecured = () => {
    $http({
      method: 'GET',
      url : 'http://Idonotexist'
    }).catch(vm.errorHandler('unsecured'))
  }
});
在我的
errorHandler
函数中,我想将取消的请求与实际错误区分开来(例如“不安全”或“未解析的主机”)

将取消器传递给错误处理程序对我来说是一件非常痛苦的事情,如果没有angularjs取消器,它将无法正确处理被浏览器取消的请求

到目前为止,以下是我在错误处理程序中得到的错误

未取消(主机未解析)请求的错误:

{
  "data": null,
  "status": -1,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "jsonpCallbackParam": "callback",
    "url": "https://Idonotexist",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    }
  },
  "statusText": ""
}
{
  "data": null,
  "status": -1,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "jsonpCallbackParam": "callback",
    "timeout": {
      "$$state": {
        "status": 1,
        "processScheduled": false,
        "pur": true
      }
    },
    "url": "https://www.googleapis.com/books/v1/volumes?q=isbn:0747532699",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    }
  },
  "statusText": ""
}
取消请求的错误:

{
  "data": null,
  "status": -1,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "jsonpCallbackParam": "callback",
    "url": "https://Idonotexist",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    }
  },
  "statusText": ""
}
{
  "data": null,
  "status": -1,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "jsonpCallbackParam": "callback",
    "timeout": {
      "$$state": {
        "status": 1,
        "processScheduled": false,
        "pur": true
      }
    },
    "url": "https://www.googleapis.com/books/v1/volumes?q=isbn:0747532699",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    }
  },
  "statusText": ""
}
我有一个包含承诺的config.timeout,但我希望避免使用它。我想将被浏览器取消的请求视为已取消的请求,即使没有取消者

下面是一个例子

有没有办法在被取消的请求上添加一个拦截器来标记

更新

这无法解决angularjs$http代码没有将错误与超时或中止分开。你可以看到


A正在运行。

是否存在
timeout
属性不足以表明请求已被取消?它可能适用于由angularjs位取消的请求,而不是由浏览器本身取消的请求(通过示例进行页面更改)。是否存在
timeout
属性不足以表明请求已被取消?它可能适用于由angularjs位取消的请求,而不是由浏览器本身取消的请求(通过示例进行页面更改)。