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. And get in java Script debugger this error:
XML Parsing Error: no root element found
Location: https://localhost:44396/Auth/Register
Line Number 1, Column 1
So, this is my sources:
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.
Just in case my 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.
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>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.