簡體   English   中英

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

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

我正在我的項目中工作,並進行注冊。 我正在嘗試使用 Ajax 處理我的數據以注冊 Controller 到操作寄存器。 並在 java 腳本調試器中得到這個錯誤:

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

所以,這是我的消息來源:

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')
        }
    })

行動:

        [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);
        }

和形式:

<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>

在調試中,我正在輸入

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

並且無法進入Action。

以防萬一我的 model:

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

我希望有任何幫助:)

您在后端使用了ValidateAntiForgeryToken屬性,因此您必須在發送請求時添加RequestVerificationToken header。

這是您可以遵循的工作演示:

看法:

<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