使用相同的URL标记有意的http请求取消api请求

时间:2017-02-08 11:55:22

标签: angularjs asp.net-web-api

这是http拦截器服务,应该使用相同的URL来处理先前请求的取消。在致电' resolve'落入“捕捉”的方法。我的角度数据服务的处理程序。 如何将此取消标记为故意,因此我的数据服务并不表示服务器端发生了错误?在这种情况下,我不想显示错误消息。

(function () {
'use strict';

angular
    .module('common')
    .factory('abortService', abortService);

abortService.$inject = ['$q', '$filter'];

function abortService($q, $filter) {

    var service = {
        request: request,
        };
    var error = "";
    var currentRequests = [];

    return service;


    function request(request) {

        if (!request.url.includes('api/')) return request;

        var deferred = $q.defer();
        request.timeout = deferred.promise;

        var existingRequests = $filter('filter')(currentRequests, { url: request.url }, true);
        existingRequests.forEach(function (request) {
            request.promiseObj.resolve('intentional_reject');//how to flag as intentional reject??
        });

        currentRequests.push({ url: request.url, promiseObj: deferred });

        return request;
    }

};})();

这是我的角度数据服务方法:

function getBalanceDueSummary(businessPartnerCode) {
        return $http({
            method: "get",
            url: "/api/BusinessPartnerAPI/GetBalanceDueSummary",
            params:
                {
                    businessPartnerCode: businessPartnerCode,
                }
        })
        .then(complete)
        .catch(failed);

        function complete(response) {
            return response.data;
        }

        function failed(error) { //here my cancellation falls into, but i have no data which indicates intentional cancellation
            //logService.exception displays a toastr message when error occurrs
            logService.exception(error, 'GetBalanceDueSummary');
            return $q.reject(error);
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经做了一些挖掘,发现了值' intentional_reject'在对象中: error.Config.timeout。$$ state.value

function getBalanceDueSummary(businessPartnerCode) {
        return $http({
            method: "get",
            url: "/api/BusinessPartnerAPI/GetBalanceDueSummary",
            params:
                {
                    businessPartnerCode: businessPartnerCode,
                }
        })
        .then(complete)
        .catch(failed);

        function complete(response) {
            return response.data;
        }

        function failed(error) {
            if(!(error.Config.timeout.$$state.value &&
                error.Config.timeout.$$state.value=='intentional_reject'))
                    logService.exception(error, 'GetBalanceDueSummary');
            return $q.reject(error);
        }
    }

我怀疑这是最好的解决方案,但我无法手动设置响应状态...