XML Parsing Error: no root element found in ASP.NET Core 5.0 Ajax
XML Parsing Error: no root element found in ASP.NET Core 5.0 Ajax
我正在做我的项目,正在注册。我正在尝试使用 Ajax 处理我的数据,以注册控制器以进行操作注册。并在 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
以防万一我的模型:
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>
}
我正在做我的项目,正在注册。我正在尝试使用 Ajax 处理我的数据,以注册控制器以进行操作注册。并在 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
以防万一我的模型:
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>
}