内部服务器错误在 post 请求中使用 Angular js 中的自定义服务

Internal Server error on post Request Using Made Custom Services in Angular js

本文关键字:js Angular 自定义 服务 错误 post 请求 内部 服务器      更新时间:2023-09-26

我是 angular 的新人,所以我尽力解决这个问题,但经过太多的斗争,我不能这就是问的原因。 实际上,我没有收到回复 $http 我不知道为什么,因为据我说这似乎是正确的。

这个问题的两个问题 1-发布请求失败 2-饼干如何存放在角度中

注册控制器.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;
});

如果有人在需要更改的地方纠正此问题,我将不胜感激。

你用错$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);
        });