0

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
});

});

Mayank Kumar
  • 313
  • 2
  • 13
  • Maybe transforming POST params could help: http://stackoverflow.com/questions/24710503/how-do-i-post-urlencoded-form-data-with-http-in-angularjs you have params in GET and data in POST. Http error code 0 may mean blank result - see in developer console in network what do you send in both cases. – changtung Jul 21 '15 at 15:57
  • My bad, I apologize for my mistake. My question wasn't clear. I want to say GET requests not aborting while POST requests abort perfectly. I will update question. – Mayank Kumar Jul 22 '15 at 06:10

0 Answers0