在Angular中中止待处理的http请求而不是中止

时间:2015-02-04 23:53:56

标签: javascript ajax angularjs http

我试图在触发新请求时在Angular中中止$ https请求。但是,Chrome的开发工具中的请求似乎仍处于待处理状态。我在这里做错了吗?

当allMarkersRequest不为null时,

allMarkersRequest.abort()被触发。所以调用deferredAbort.resolve(),但程序仍然会通过.then()。

var allMarkersRequest = null;

function getAllMarkersForSearch() {
    if (allMarkersRequest) {
        allMarkersRequest.abort();
        console.log('Aborting all markers request');
    }

    var deferredAbort = $q.defer();

    var url = Routing.generate('ajax_search_geo_all_markers');

    var req = $http({
        method: 'POST',
        url: url
    });

    allMarkersRequest = req.then(function (data) {
        createMarkers(data.data);

        $rootScope.$broadcast('map:markers:updated', markers);
    }, function (error) {
        // Display error message
    });

    allMarkersRequest.abort = function() {
        deferredAbort.resolve();
    };

    allMarkersRequest.finally(function () {
        allMarkersRequest.abort = angular.noop;
        deferredAbort = req = allMarkersRequest = null;
    });
}

1 个答案:

答案 0 :(得分:0)

我没有在请求中包含timeout参数

var allMarkers Request = null;

function getAllMarkersForSearch() {
    if (allMarkersRequest) {
        allMarkersRequest.abort();
        console.log('Aborting all markers request');
    }

    var deferredAbort = $q.defer();

    var url = Routing.generate('ajax_search_geo_all_markers');

    var req = $http({
        method: 'POST',
        url: url,
        timeout: deferredAbort
    });

    allMarkersRequest = req.then(function (data) {
        createMarkers(data.data);

        $rootScope.$broadcast('map:markers:updated', markers);
    }, function (error) {
        // Display error message
    });

    allMarkersRequest.abort = function() {
        deferredAbort.resolve();
    };

    allMarkersRequest.finally(function () {
        allMarkersRequest.abort = angular.noop;
        deferredAbort = req = allMarkersRequest = null;
    });
}