【问题标题】:$http post via an angularjs service$http 通过 angularjs 服务发布
【发布时间】:2015-09-02 06:01:35
【问题描述】:

我使用下面的代码。 dataService 的内容是我的应用程序中所有可能的 $http 。在控制器中,我使用了这个函数。该函数调用一个 Web Api 服务,这个服务被调用并返回正确的响应。在函数customerToggleSuccess 中,数据是undefined。我不明白为什么。

(function () {
    angular.module('myApp')
        .factory('dataService', ['$q', '$http']);

    function dataService($q, $http,) {

        return {
            customerToggleActive: customerToggleActive
        };

        function customerToggleActive(customerId, index) {

            var customer = {
                "id": customerId,
                "index": index
            };

            return $http({
                method: 'POST',
                url: 'api/customer/validtoggle/',
                headers: {
                },
                transformResponse: function (data, headers) {
                },
                data: customer
            })
            .then(customerToggleData)
            .catch(customerToggleError)
        }

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

        function customerToggleError(response) {
        }
    }

}());

(function () {

    angular.module('myApp')
        .controller('customerController', ['$scope', 'dataService', '$http', '$log', CustomerController]);

    function CustomerController($scope, dataService, $http, , $log) {
        var vm = this;

        vm.activeToggle = function (customerId, index) {

            dataService.customerToggleActive(customerId, index)
                .then(customerToggleSuccess)
                .catch(customerToggleCatch)
                .finally(customerToggleComplete);

            function customerToggleSuccess(data) {
                $log.info(data);
            }

            function customerToggleCatch(errorMsg) {
            }

            function customerToggleComplete() {
            }

        } 

    }

}());

【问题讨论】:

  • 在 $http 中配置了一个 transformRequest 函数,但不返回任何内容。也许这是问题? link

标签: javascript jquery angularjs asp.net-web-api


【解决方案1】:

只需返回 $http 承诺,

  return $http({
             method: 'POST',
             url: 'api/customer/validtoggle/',
             headers: {
             },
             transformResponse: function (data, headers) {
             },
             data: customer
         })

您可以通过以下方式访问它,

dataService.customerToggleActive(customerId, index)
      .then(function (response) {
             // do your stuff
      })

或者,你可以这样做,

function dataService($q, $http,) { 
    var defer = $q.defer();
    ....
    ....
    $http({
         method: 'POST',
         url: 'api/customer/validtoggle/',
         headers: {
         },
         transformResponse: function (data, headers) {
         },
         data: customer
     })
     function customerToggleData(response) {
        defer.resolve (response.data);
    }

    function customerToggleError(response) {
        defer.reject(response);
    }
    return defer.promise;
}

【讨论】:

  • 第二种情况没有变化,第一种情况如何对待promise?
  • 你可以像之前一样使用 Promise
【解决方案2】:

像这样,

(function () {
        angular.module('myApp')
            .factory('dataService', ['$q', '$http']);

        function dataService($q, $http,) {

            return {
                customerToggleActive: customerToggleActive
            };

            function customerToggleActive(customerId, index) {

                var customer = {
                    "id": customerId,
                    "index": index
                };

                return $http({
                    method: 'POST',
                    url: 'api/customer/validtoggle/',
                    headers: {
                    },
                    transformResponse: function (data, headers) {
                    },
                    data: customer
                })

            }      
        }

    }());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多