我的API服务不断陷入错误情况-AngularJS

时间:2019-05-21 17:36:38

标签: javascript angularjs

服务

app.service('service', function ($http, $q) {

    //Testing code
    var self = this;

    self.login = (user) => {
        let defer = $q.defer();
        let login  = {
            username: user.username,
            password: user.password
        };

        $http({
            method: 'POST',
            url: `http://1.1.1.1/login`,
            data: login
        }).then(
            (response) => {
                defer.resolve(response.data)
            },
            (error) => defer.reject(error)
        );

        return defer.promise;
    };

});

控制器

angular.module('app').controller('controller',
    ['$scope', '$rootScope', '$location', 'service','$log' ,'$q',
    function($scope, $rootScope, $location, service, $log, $q) {

        console.log('controller called');

        $scope.changeView = function(view) {
            $location.path(view);
        };

        $scope.user = {username: 'john@nuc.com', password: 'qqq'};
        console.log($scope.user);

        service.login($scope.user)
        .then(
            (response) => {
                console.log('success');
            },
            (error) => console.log('error' + error.data)
        );

    }]);

结果

我看到了

{location: "http://ddddd.com/hom…zg2fQ.D9pDqUBD3bcKnH_bPoGCpd-odxJ48LUZTKOGZZnV0Y0"}

我不知道为什么要进入error案例部分,但是我似乎从服务器获得了正确的响应。

有人可以对此进行说明吗?

1 个答案:

答案 0 :(得分:1)

该服务使用deferred anti-pattern。虽然这不是问题的根源,但请解决它并添加console.log语句:

onboardingApp.service('apiService', function ($http, $q, $localStorage) {

    //Testing code
    var self = this;

    self.login = (user) => {
        ̶l̶e̶t̶ ̶d̶e̶f̶e̶r̶ ̶=̶ ̶$̶q̶.̶d̶e̶f̶e̶r̶(̶)̶;̶
        let login  = {
            username: user.username,
            password: user.password
        };

        return $http({
            method: 'POST',
            url: `http://ddd.com/login`,
            data: login
        }).then(
            (response) => {
                console.log("success", response);
                setAuthToken(response);
                return response.data;
            },
            (error) => {
               console.log("error",error);
               throw error;
            }
        );
    };    
});

这应该可以帮助您诊断问题。


  

成功登录后,它应该返回状态码200而不是302。我如何调整您的代码以成功地接受302而不是错误?

要将拒绝的承诺转换为成功的承诺,只需将return的值转换为拒绝处理程序即可:

       return $http({
            method: 'POST',
            url: `http://ddd.com/login`,
            data: login
        }).then(
            (response) => {
                console.log("success", response);
                setAuthToken(response);
                return response.data;
            },
            (error) => {
               console.log("error",error);
               if (error.status==302) {
                   console.log("Converting erroneous API response");
                   error.status==200;
                   return error.data;
               };
               //ELSE
               throw error;
            }
        );