简体   繁体   English

XML 解析错误:在 ASP.NET 核心 5.0 Ajax 中找不到根元素

[英]XML Parsing Error: no root element found in ASP.NET Core 5.0 Ajax

I'm working at my project, and doing Sign-up.我正在我的项目中工作,并进行注册。 I am trying to process my data with Ajax to Sign-up Controller to Action Register.我正在尝试使用 Ajax 处理我的数据以注册 Controller 到操作寄存器。 And get in java Script debugger this error:并在 java 脚本调试器中得到这个错误:

XML Parsing Error: no root element found
Location: https://localhost:44396/Auth/Register
Line Number 1, Column 1

So, this is my sources:所以,这是我的消息来源:

Js: JS:

let fD = new FormData();
    fD.append('Email', email);
    fD.append('Name', name);
    fD.append('Password', pass);

    $('#loading').addClass('processing');
    $.ajax({
        type: 'POST',
        url: '@Url.Action("Register")',
        data: fD,
        processData: false,
        contentType: false,
        success: function(res, status, xhr) {
            $('#loading').removeClass('processing');
            let result = xhr.getResponseHeader("registration_result")
            if (result === "ok") {
                createMessage('zmdi-check','You have successfully registered in our site');
                Timer();
            }
            else if (result === "failed") {
                createMessage('zmdi-close','This email address already exists</br>Please choose a unique one');
                form.email.classList.add('error');
            }
            else // "error"
                createMessage('zmdi-close','An error occurred while registering</br>Please try again')
        }
    })

Action:行动:

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Register(RegisterViewModel model)
        {
            var user = new User();
            var valid = ValidateUser(model);
            if (!valid)
                Response.Headers.Add("registration_result", new StringValues("error"));
            else if (FindUser(model.Email))
                Response.Headers.Add("registration_result", new StringValues("failed"));
            else
            {
                user = _catalog.AddUserOrDefault(new UserRegistrationDto
                {
                    Email = model.Email,
                    Name = model.Name,
                    Password = model.Password
                });
                Response.Headers.Add("registration_result", new StringValues("ok"));
            }

            if (user != null)
            {
                await _signInManager.SignInAsync(user, false);
            }

            return Ok(model);
        }

and form:和形式:

<form method="POST" name="signUp" class="form register-form" id="register-form" onsubmit="return false;">
                <label class="label-input" for="">
                    <i class="zmdi far fa-user icon-modify"></i>
                    <input class="auth-input" type="text" placeholder="Name" name="name" id="name">
                </label>
                <label class="label-input" for="">
                    <i class="zmdi far fa-envelope icon-modify"></i>
                    <input class="auth-input" type="email" placeholder="Email" name="email" id="email">
                </label>
                <label class="label-input" for="">
                    <i class="zmdi fas fa-lock icon-modify"></i>
                    <input class="auth-input" type="password" placeholder="Password" name="pass" id="pass">
                </label>
                <button class="btn btn-secondary" id="signup-button">sign up</button>
            </form>

In debug I'm entering for在调试中,我正在输入

$('#loading').addClass('processing');

And can't enter to Action.并且无法进入Action。

Just in case my model:以防万一我的 model:

public class RegisterViewModel
    {
        public string Email { get; set; }
     
        public string Name { get; set; }
        
        public string Password { get; set; }
    }

I hope for any help:)我希望有任何帮助:)

You used ValidateAntiForgeryToken attribute in your backend so you must add RequestVerificationToken header when sending request.您在后端使用了ValidateAntiForgeryToken属性,因此您必须在发送请求时添加RequestVerificationToken header。

Here is a working demo you could follow:这是您可以遵循的工作演示:

View:看法:

<form method="POST" name="signUp" class="form register-form" id="register-form" onsubmit="return false;">
    <label class="label-input" for="">
        <i class="zmdi far fa-user icon-modify"></i>
        <input class="auth-input" type="text" placeholder="Name" name="name" id="name">
    </label>
    <label class="label-input" for="">
        <i class="zmdi far fa-envelope icon-modify"></i>
        <input class="auth-input" type="email" placeholder="Email" name="email" id="email">
    </label>
    <label class="label-input" for="">
        <i class="zmdi fas fa-lock icon-modify"></i>
        <input class="auth-input" type="password" placeholder="Password" name="pass" id="pass">
    </label>
    <button class="btn btn-secondary" id="signup-button" onclick="Test()">sign up</button>
</form>
<div id="loading"></div>
@section Scripts
{
    <script>
        function Test() {
            var email = $("#email").val();
            var name = $("#name").val();
            var pass = $("#pass").val();
            let fD = new FormData();
            fD.append('Email', email);
            fD.append('Name', name);
            fD.append('Password', pass);   
    $('#loading').addClass('processing');
    $.ajax({
        type: 'POST',
        url: '@Url.Action("Register")',
        data: fD,
        //be sure add this...
        headers: {
            RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
        },
        processData: false,
        contentType: false,
        success: function(res, status, xhr) {
            $('#loading').removeClass('processing');
            let result = xhr.getResponseHeader("registration_result")
            if (result === "ok") {
                createMessage('zmdi-check','You have successfully registered in our site');
                Timer();
            }
            else if (result === "failed") {
                createMessage('zmdi-close','This email address already exists</br>Please choose a unique one');
                form.email.classList.add('error');
            }
            else // "error"
                createMessage('zmdi-close','An error occurred while registering</br>Please try again')
        }
    })
    }
    </script>
}

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

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