【问题标题】:Angular JS : Post call is going in Success as well as Error MethodAngular JS:发布调用成功以及错误方法
【发布时间】:2017-02-13 13:34:44
【问题描述】:

我知道我问这个时看起来很愚蠢,但我无法弄清楚这一点。

我编写了一个服务来处理对服务器的 post 调用。 $q 服务将承诺返回给调用该服务的控制器函数。

服务:

app.service('AjaxService', ['$http','$q','$log', function($http,$q,$log) {
    return {
        getSearchresultPost : function(url,data){
            var defer = $q.defer();
            $http.post(url, data)
            .then(function(data, status, header, config){
                defer.resolve(data);

            }).then(function(data, status, header, config){
                defer.reject(data);
            });
            return defer.promise;
        }
    };
}]);

控制器

app.controller("kitGuideNavigationController",['$scope','$window','$timeout','AjaxService',function($scope,$window,$timeout,AjaxService){
 AjaxService.getSearchresultPost("/services/knowledge/getProducts",pathToCall)
        .then(function(data){
            console.log("Data ",data);
        }).then(function(data){
            console.log("Some Error Occured");
        });
}]);

当我尝试运行代码时,我会打印两个控制台。

我不明白出了什么问题。有人可以帮忙吗?

【问题讨论】:

  • 您可以使用 $http.post() 返回的差异,除非您有嵌套调用等花哨的东西,否则无需创建自定义差异

标签: angularjs angularjs-directive angularjs-scope angular-ui-router


【解决方案1】:

将第二个“then”更改为“catch”,应该可以解决这个问题。在这两种情况下都必须这样做。

    app.controller("kitGuideNavigationController",['$scope','$window','$timeout','AjaxService',function($scope,$window,$timeout,AjaxService){
 AjaxService.getSearchresultPost("/services/knowledge/getProducts",pathToCall)
        .then(function(data){
            console.log("Data ",data);
        }).catch(function(data){
            console.log("Some Error Occured");
        });
}]);

更新

正如我所见,您使用的是 $http,请检查 here

【讨论】:

  • 非常感谢@Fribu :)
  • @vaibhav 为什么要两次调用.then()?如果是我,我只会使用一个 .then() 并使用 if 条件来决定是解决还是拒绝最初的承诺。
  • 实际上我也必须从 Catch 编写代码,这就是为什么我使用第二个然后进行错误处理的原因。
  • @vaibhav 太好了,我可以帮忙,你可以接受答案
【解决方案2】:

您可以更改您的服务并像这样在 $http.post 中传递第二个参数(错误函数)(文档:https://docs.angularjs.org/api/ng/service/$http):

app.service('AjaxService', ['$http','$q','$log', function($http,$q,$log) {
    return {
        getSearchresultPost : function(url,data){
            var defer = $q.defer();
            $http.post(url, data)
            .then(function(data, status, header, config){
                defer.resolve(data);

            }, function(error, status, header, config){
                defer.reject(error);
            });
            return defer.promise;
        }
    };
}]);

在你的控制器中你也可以传递第二个参数:

app.controller("kitGuideNavigationController",['$scope','$window','$timeout','AjaxService',function($scope,$window,$timeout,AjaxService){
 AjaxService.getSearchresultPost("/services/knowledge/getProducts",pathToCall)
        .then(function(data){
            console.log("Data ",data);
        }, function(error){
            console.log("Some Error Occured", error);
        });
}]);

【讨论】:

    【解决方案3】:

    没有必要用$q.defer 制造一个promise,因为$http 服务已经返回了一个promise:

    app.service('AjaxService', ['$http','$q','$log', function($http,$q,$log) {
        return {
            getSearchresultPost : function(url,data){
                //var defer = $q.defer();
                var promise = $http.post(url, data)
                //.then(function(data, status, header, config){
                .then(function (response) {
                    var data = response.data;
                    //defer.resolve(data);
                    //return data to resolve
                    return data;    
                //}).then(function(data, status, header, config){
                }).catch(function(response) {
                    var data = response.data;
                    //defer.reject(data);
                    //throw data to reject with value
                    throw data;
                });
                //return defer.promise;
                return promise;
            }
        };
    }]);
    

    还要注意.then.catch 方法使用response 对象调用它们的函数,而不是data,但是将data 属性返回(或抛出)到处理程序会创建一个新的promise,它解析(或拒绝)具有该值。

    AjaxService.getSearchresultPost(url,pathToCall)
        .then(function(data){
            console.log("Data ",data);
        //}).then(function(data){
        }).catch(function(data) {
            console.log("Some Error Occured");
        });
    

    欲了解更多信息,请参阅AngularJS $q Service API Reference - Chaining Promises

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多