【发布时间】:2021-08-26 22:01:16
【问题描述】:
我正在处理我的项目,并且正在注册。我正在尝试使用 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; }
}
希望能帮到你:)
【问题讨论】:
-
嗨@Nazmiev Kamil,实际上我没有遇到同样的问题。你能分享一下哪一行犯了这样的错误吗?此外,请务必在 ajax 中添加
RequestVerificationToken标头,否则您将在控制台面板中收到 400 错误。
标签: javascript ajax asp.net-mvc asp.net-core asp.net-ajax