I have written an angular service/factory for api request one with get request and one with post request.I am getting very strange behaviour.
I am not able to cancel/abort the GET request with below code and GET request runs to completion without getting cancelled.
var joblist = jobsService.getJobList($scope.jobListData);
joblist.then($scope.jobListSuccess,$scope.jobListError);
joblist.abort('cancelling get request');
But I am able to cancel/abort the POST request with below code and it is getting cancelled and return http status code 0 and thats what i want.
var addNewJob = jobsService.addJob($scope.addNewJobData);
addNewJob.then($scope.addNewJobSuccess,$scope.addNewJobError);
addNewJob.abort('cancelling post request');
I am wondering why GET request is not aborting/cancelling while POST request is aborting/cancelling with the similar code.
.factory('jobsService',function($http, $q, API_URL_PREFIX){
//return job list
this.getJobList = function(data){
var deferredAbort = $q.defer();
// Initiate the AJAX request.
var request = $http({
method: 'get',
url: API_URL_PREFIX + 'jobs',
params : data,
timeout: deferredAbort.promise
});
var promise = request.then(
function( response ) {
return( response.data );
},
function() {
return( $q.reject( 'Something went wrong' ) );
}
);
promise.abort = function(reason) {
console.log(reason);
deferredAbort.resolve();
};
promise.finally(
function() {
promise.abort = angular.noop;
deferredAbort = request = promise = null;
}
);
return( promise );
}
//add job
this.addJob = function(data){
var deferredAbort = $q.defer();
// Initiate the AJAX request.
var request = $http({
method: 'post',
url: API_URL_PREFIX + 'jobs',
data: data,
timeout: deferredAbort.promise
});
var promise = request.then(
function( response ) {
return( response.data );
},
function() {
return( $q.reject( 'Something went wrong' ) );
}
);
promise.abort = function(reason) {
console.log(reason);
deferredAbort.resolve();
};
promise.finally(
function() {
promise.abort = angular.noop;
deferredAbort = request = promise = null;
}
);
return( promise );
}
// Return the public API.
return({
getJobList: this.getJobList,
addJob: this.addJob
});
});