1

我是 Angular 的新手,所以我尽力解决这个问题,但经过太多努力,我不能这就是为什么要问。实际上,我没有得到回复$http,因为我不知道为什么,因为在我看来这似乎是正确的。

此问题的两个问题 1- 发布请求失败 2- CookieStore 的角度如何

注册控制器.js

app.controller('signUpController', function ($rootScope, $scope, signUpService, $location) {

    $scope.addUser = function(){


        var user = {
            "email": $scope.EmailInfo,
            "confirm_email": $scope.ConfirmEmailInfo,
            "password": $scope.PasswordInfo,
            "confirm_password": $scope.ConfirmPasswordInfo,
            "first_name": $scope.FirstnameInfo,
            "last_name": $scope.LastNameInfo,
            "dob": $scope.dateOfBirth,
            "gender": $scope.radioValue
        }

        signUpService.addNewUser(user).then(function(response) {

            if(response.status == 500) {
                $rootScope.loggedIn = false;
                $location.url('/SignUp');
            }else {
                $rootScope.loggedIn = true;
                $location.url('/Stores');
                console.log(data);
            }
            console.log(response);

        });

    }
});

注册服务.js

app.factory('signUpService', function($http, $cookieStore) {

    var signUpServices = {

        addNewUser : function(user) {

            var promise = $http({
                method:'POST',
                url: "/user/register?account_id=" + utils.urlParam('account_id') ,
                data: user
            }).then(function(response) {

                    $cookieStore.put("token", response["token"]); // is it write that i used also check please
                    console.log(response.data);
                    return response.data;
                },
                    function(reason) {
                        alert("There was a problem");
                    });

            return promise;
        }
    };
    return signUpServices;
});

错误

POST http://localhost:3000/user/register?account_id=0 500 (Internal Server Error) angular.js:10159
(anonymous function) angular.js:10159
sendReq angular.js:9980
$http.serverRequest angular.js:9757
deferred.promise.then.wrappedCallback angular.js:7303
deferred.promise.then.wrappedCallback angular.js:7303
(anonymous function) angular.js:7340
Scope.$eval angular.js:8685
Scope.$digest angular.js:8548
Scope.$apply angular.js:8771
(anonymous function) angular.js:13970
f.event.dispatch jquery.js:3
h.handle.i

更新但同样的问题

app.factory('signUpService', function($http) {

    var signUpServices = {
        addNewUser : function(user) {

            var promise = $http({method: 'POST', url: "/user/register?account_id=" + utils.urlParam('account_id'),
                data: user}).

                success(function(data, status, headers, config) {
                    console.log("response");
                    alert(data);
                }).

                error(function(data, status, headers, config) {
                    alert(status);
                });

            return promise;
        }
    };
    return signUpServices;
});

如果有人在需要更改的地方纠正此问题,我将非常感谢您。

4

1 回答 1

1

你使用 $http 错误

$http({method: 'GET', url: '/someUrl'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }). 
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

以下是如何在每个请求上获取 cookie:

        $http.defaults.transformRequest.push(function (data) {
            data.csrfToken = $browser.cookies().csrfToken;
            return angular.toJson(data);
        });
于 2013-11-09T13:49:45.097 回答