有没有人在AngularJS(v1.0.7)和Chrome(版本30.0.1599.114)中看到此错误,其中取消http GET请求会将套接字置于挂起状态,从而最大限度地利用chrome中的线程池?
代码:
if ($scope.canceler !== undefined) {
$scope.canceler.resolve();
$scope.canceler = undefined;
}
$scope.canceler = $q.defer();
$http.get("/apicall", {
cache: myCache,
timeout: $scope.canceler.promise
}).success(function (results) {
}).
error(function (result) {
});
答案 0 :(得分:3)
您应该将AngularJS更新为1.1.5才能取消http调用。 参考:In AngularJS, how to stop ongoing $http calls on query change
这是工作代码和JS小提琴。我已经使用AngularJS 1.2.0和Chrome 32.0.1700.0 canary进行了测试。
function Ctrl($rootScope, $scope, $http, $q, $timeout) {
var canceler = $q.defer();
console.log("Calling...");
$http.get("/echo/json", {
//Will return data after 5 seconds passed
data: {json: {id: "123"}, delay: 5},
timeout: canceler.promise
}).success(function (results) {
console.log("Success");
console.log(results);
}).
error(function (result) {
console.log("Error");
console.log(result);
});
$timeout(function () {
console.log("1 second passed");
// now, cancel it (before it may come back with data)
$rootScope.$apply(function () {
console.log("Canceling..");
canceler.resolve();
});
}, 1000);
}
请求已取消状态。