I'm using Angular version 1.2.1 and I'm attempting to implement cancelation in my application when a new request of the same type is initialized. I used the code examples here but I couldn't get it to work. I think this is due to the fact that I am using $http.jsonp().
When I cancel a jsonp request the .then callbacks are never called (as expected) but the request is not cancelled and when the response comes I get an undefined error in the console (the jsonp callback function no longer exists).
How do I avoid getting this console error? Is it possible to cancel a jsonp request alltogheter?
http://jsfiddle.net/HB7LU/8668/
HTML:
<div ng-controller="MyCtrl">
<span ng-bind="status"></span>
<hr>
<a href ng-click="doStuff()">
Start
</a>
<hr>
<a href ng-click="cancel()">
Cancel
</a>
</div>
JS:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, $q, $http) {
$scope.status = "Idle";
$scope.doStuff = function() {
var canceller = $q.defer();
$scope.status = "Ongoing";
$scope.getting = $http.jsonp('http://cruisebookingapi.ving.se/cruisePackage/search/1/STO/141220/false/20141211/42,42/-1?callback=JSON_CALLBACK', {timeout: canceller.promise});
$scope.getting.then(function(data) {
$scope.status = "Finished";
console.log(data);
});
$scope.cancel = function() {
$scope.status = "Cancelled";
canceller.resolve('Cancel all the things');
};
};
}