在这个$ http请求中有什么不对吗?

时间:2016-12-09 05:16:14

标签: angularjs http http-post

以下是代码:

(function () {

angular.module('PillApp').factory('AuthFactory',
    ['$q', '$timeout', '$http', 'Flash', '$state',
        function ($q, $timeout, $http, Flash, $state) {

            // create user variable
            var user = null;
            // return available functions for use in the controllers
            return ({
                isLoggedIn: isLoggedIn,
                getUserStatus: getUserStatus,
                login: login,
                logout: logout,
                register: register
            });

            function isLoggedIn() {
                if(user) {
                    return true;
                } else {
                    return false;
                }
            }

            function getUserStatus(callback) {
                $http.get('/status/user')
                // handle success
                    .then(function (data) {
                        //console.log(JSON.stringify(data));
                        if(data.data !== ''){
                            user = true;
                            callback(user);
                        } else {
                            user = false;
                            callback(user);
                        }
                    });
            }

            function login(userProfile) {

                // create a new instance of deferred
                var deferred = $q.defer();

                console.log("Authentication Service - to send login to server");
                // send a post request to the server
                $http.post('/login',
                    userProfile)
                // handle success
                    .success(function (data, status) {
                        console.log("Authentication response received");
                        if(status == 200){
                            getUserStatus(function(result){
                                if(result){
                                    deferred.resolve();
                                    $state.go('A');
                                }else{
                                    deferred.reject();
                                    Flash.create('danger', "Ooops having issue logging in!", 0, {class: 'custom-class', id: 'custom-id'}, true);
                                    $state.go('A');
                                }
                            });
                        } else {
                            user = false;
                            Flash.clear();
                            Flash.create('danger', "Ooops having issue logging in!", 0, {class: 'custom-class', id: 'custom-id'}, true);
                            deferred.reject();
                        }
                    })
                    // handle error
                    .error(function (data) {
                        console.log("Authentication error - any flash messsage?");
                        user = false;
                        Flash.clear();
                        Flash.create('danger', "Ooops having issue logging in!", 0, {class: 'custom-class', id: 'custom-id'}, true);
                        deferred.reject();
                    });

                // return promise object
                return deferred.promise;
            }

            function logout() {

                // create a new instance of deferred
                var deferred = $q.defer();

                // send a get request to the server
                $http.get('/logout')
                // handle success
                    .success(function (data) {
                        user = false;
                        deferred.resolve();
                    })
                    // handle error
                    .error(function (data) {
                        user = false;
                        deferred.reject();
                    });

                // return promise object
                return deferred.promise;

            }

            function register(username, password) {

                // create a new instance of deferred
                var deferred = $q.defer();
                console.log("Sending registration to server");
                // send a post request to the server
                $http.post('/register',
                    {username: username, password: password})
                // handle success
                    .success(function (data, status) {
                        if(status){
                            console.log('Registration successful', data, status);
                            deferred.resolve();
                        } else {
                            console.log('Registration unsuccessful', data);
                            Flash.closeFlash();
                            Flash.create('danger', "Can't register with us!", 0, {class: 'custom-class', id: 'custom-id'}, true);
                            deferred.reject();
                        }
                    })
                    // handle error
                    .error(function (data) {
                        console.log('Registration error', data);
                        Flash.clear();
                        Flash.create('danger', "Ooops something went wrong!", 0, {class: 'custom-class', id: 'custom-id'}, true);
                        deferred.reject();
                    });

                // return promise object
                return deferred.promise;

            }

        }]);
})();

错误:

angular.js:14324 TypeError: $http.post(...).then is not a function
    at Object.login (http://ec2-52-77-252-229.ap-southeast-1.compute.amazonaws.com:3001/services/auth.factory.js:55:22)
    at AppCtrl.vm.login (http://ec2-52-77-252-229.ap-southeast-1.compute.amazonaws.com:3001/app.controller.js:35:25)
    at fn (eval at compile (http://ec2-52-77-252-229.ap-southeast-1.compute.amazonaws.com:3001/bower_components/angular/angular.min.js:236:285), <anonymous>:4:167)
    at e (http://ec2-52-77-252-229.ap-southeast-1.compute.amazonaws.com:3001/bower_components/angular/angular.min.js:281:195)

我尝试了$ http.post()。也成功了,它给出了成功未定义的错误

比我试过$ http.post()。然后它也给出了错误,就像那时未定义

我以正确的方式注入了$ http。它在localhost上工作但在生产过程中它会产生这种错误。

3 个答案:

答案 0 :(得分:0)

您需要以这种方式定义包含http post指令的函数:

 $scope.getUserCallback = function (callback) {
            $http.get('/status/user')
            // handle success
                .then(function (data) {
                    //console.log(JSON.stringify(data));
                    if(data.data !== ''){
                        user = true;
                        callback(user);
                    } else {
                        user = false;
                        callback(user);
                    }
                });
        };

然后你可以从脚本里面的任何地方调用它作为

$scope.getUserCallback();

来自像这样的html元素

 <my element ng-click="getUserCallback()"></my element>

答案 1 :(得分:-1)

由于$http.post,我认为CORS来电失败了。您可能需要启用CORS或在请求中发送相应的HTTP标头。您收到的错误是因为$http.post未返回可执行下一个successthen方法的预期对象。

它可以在localhost上正常运行,因为本地开发环境中没有CORS限制。

答案 2 :(得分:-2)

而不是

  

$ http.post(&#39; /登录&#39;,USERPROFILE)。然后()

尝试使用

  

http.post(&#39; /登录&#39;,USERPROFILE)。.success(功能(响应{})误差(函数(误差){})