为什么可以't我取消了这个Angular HTTP请求
Why can't I cancel this Angular HTTP request?
如果HTTP请求超时,我将使用从这篇SO文章中找到的代码在请求中期取消它们:
var canceller = $q.defer();
$timeout(function() {
canceller.resolve();
alert("HTTP request failed.");
}, 5000);
$http({
url: endpoint + "/encode",
timeout: canceller.promise,
data: {
post: posts.post[id]
}
}).success(successFunction);
但是,我的控制台中一直有ReferenceError: timeout is not defined
。我在这里可能做错了什么?
因此,如果没有plunker,很难复制,但我成功地使用您的代码取消了http请求。Plunker here
Controller.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $q, $http, $timeout) {
$scope.msg = 'Not done it';
var canceller = $q.defer();
$scope.doThis = function() {
$timeout(function() {
canceller.resolve();
$scope.msg = "I cancelled it";
}, 1);
$scope.msg = "I did it";
var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
var request = $http.get( url, {timeout: canceller.promise});
request.success(function(results) {
$scope.msg = "I loaded some data";
$scope.data = results;
});
}
});
view.html
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<p>{{msg}}</p>
<a href="" ng-click="doThis()">Do This</a>
<p>
{{data}}
</p>
</body>
相关文章:
- 如何判断请求/承诺是否需要超过5秒才能完成,angular js
- Angular js-尚未添加PUT请求到服务的URL
- 遇到400错误请求(Angular+WebAPI)的问题
- Angular,对飞行前请求的响应
- 从C#应用程序向Angular页面发送帖子请求
- 从 Angular 数据工厂中的 HTTP POST 请求接收未定义的结果
- Angular 2:如何在 GET 和 POST 请求中设置 double、float、int 和布尔类型参数
- 处理 Angular 中的非幂等$http请求
- 使 Angular JS 实时 ajax 请求消耗更少
- Angular 中的 HTTP 服务将 POST 数据作为请求正文的键发送,没有值
- 读取Angular应用程序中请求的url
- 使用Angular/Rails发送带有params的http请求
- Angular 2:实现HTTP POST请求
- Angular JS-侦听或绑定$http请求
- 对php的Angular post请求失败
- 如何从$http请求延续中获得显示在angular ui.grid中的数据
- 如何使用scalajs-angular获取请求的http状态代码
- 在完成加载angular js Scope元素、Directives和Http请求后是否发生任何事件
- 如何在 Nginx 中将所有 Angular 请求重定向到 index.html
- Angular请求api和令牌