注册成功后无法获得响应

时间:2016-02-23 08:24:42

标签: javascript angularjs rest asp.net-web-api2

我有一个Web API 2注册方法如下: -

    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();

    }

和一个角度控制器发布到此api,如下所示: -

angular.module('monfusportsstoreApp')
.controller("registerCtrl", function($scope, UserService) {

    var vm = this;

    vm.register = register;
    vm.Error = '';
    function register() {
            UserService.createUser(vm.user)
                .then(function (response) {
                    console.log(response.data);
                    console.log(response.status);
                    if (response.success) {
                        console.log('Registration successful');
                        $location.path('/registerSuccess');
                    } else {
                        errorMessages = parseErrors(response);
                        for (var i = 0; i < errorMessages.length; i++) {
                            vm.Error += errorMessages[i];
                        }

                    }
                });
    }

    function parseErrors(response) {
        var errors = [];
        for (var key in response.ModelState) {
            for (var i = 0; i < response.ModelState[key].length; i++) {
                errors.push(response.ModelState[key][i]);
            }
        }
        return errors;
    }        

})

,UserService如下: -

angular.module("monfusportsstoreApp")
.factory("UserService", function($http, ENDPOINT_URI){

    var url = ENDPOINT_URI + '/api/Account/';
    var errorMessage = [];

    return {

        getAllUsers: function() {
            return $http.get(url).then(handleSuccess, handleError('Error getting all users'));
        },

        createUser: function(user) {
            return $http.post(url + "/Register", user)
                .then(handleSuccess, handleError);
        }

    }        

    function handleSuccess(res) {
        return res.data;
    }

    function handleError(res) {
        return res.data;
    }

});

现在我正在设法正确捕获响应错误并显示消息,但是当Register API成功并发送OK时,响应总是为空,因此这段代码

                        if (response.success) {
                        console.log('Registration successful');
                        $location.path('/registerSuccess');

永远不会被触发。

如何捕获状态200 OK以便我可以将用户重定向到RegisterSuccess页面?

感谢您的帮助和时间!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

在UserService中,我需要返回

    function handleSuccess(res) {
        return res.status;
    }

这样在控制器中,我可以捕获响应状态

        function register() {
            UserService.createUser(vm.user)
                .then(function (response) {
                    if (response == 200) {
                        console.log('Registration successful');
                        $location.path('/registerSuccess');
                    } else {
                        errorMessages = parseErrors(response);
                        for (var i = 0; i < errorMessages.length; i++) {
                            vm.Error += errorMessages[i];
                        }

                    }
                });
    }