简体   繁体   English

成功注册后未得到响应

[英]Not getting response when register is successful

I have a Web API 2 register method as follows :- 我有一个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();

    }

and an angular Controller that posts to this api as follows :- 和发布到此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;
    }        

})

and the UserService is as follows :- 和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;
    }

}); });

Now I am managing to trap the response error correctly and displaying the message, however when the Register API is successful and sends the OK, the response is always empty, and as so this piece of code 现在,我正在设法正确捕获响应错误并显示消息,但是,当Register API成功并发送OK时,响应始终为空,因此这段代码

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

is never triggered. 永远不会触发。

How can I trap the status 200 OK so that I can redirect the user to the RegisterSuccess page? 如何捕获状态200 OK,以便可以将用户重定向到RegisterSuccess页面?

Thanks for your help and time! 感谢您的帮助和时间!

I have solved this question. 我已经解决了这个问题。

In the UserService, I needed to return 在UserService中,我需要返回

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

so that in the controller, I can capture the response 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];
                        }

                    }
                });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM