Trying to do some validation on values entered on the .Net Core Identity Register page using an Ajax call.
Here is my code
Register.cshtml
button code -
<button type="button" class="btn btn-primary" id="Submit" >Create Account</button>
call code -
$("#Submit").on('click', function (event) {
debugger;
var urlVal = $(location).attr('href') + "?handler=CheckvcNumber"
debugger;
$.ajax({
Type:"POST",
url: urlVal,
beforeSend: function (xhr) {
xhr.setRequestHeader("RequestVerificationToken",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: {
"vcNumber": $("#VCNumberInput").val()
},
async: false,
success: function (response) {
debugger;
$('#RegisterForm').submit();
},
failure: function (response) {
debugger;
alert(JSON.stringify(response));
},
error: function (response) {
debugger;
alert(JSON.stringify(response));
}
});
});
The urlVal = 'http://localhost:53196/Identity/Account/Register?handler=CheckvcNumber'
In Register.cshtml.cs
[Authorize(Roles = "Admin")]
public class RegisterModel : PageModel
. . .
public async Task<JsonResult> OnPostCheckvcNumberAsync(string vcNumber)
{
if (string.IsNullOrEmpty(vcNumber)) throw new ArgumentException(nameof(vcNumber));
return new JsonResult(new { message = "Unauthorized" });
}
When I run the code it goes straight to the successfunction in the Ajax call. Doesn't go to the OnPostCheckvcNumberAsyncmethod in Register.cshtml.cs
Hope someone can help!
Thanks
Doesn't go to the OnPostCheckvcNumberAsyncmethod in Register.cshtml.cs
Try to change Type
lowercase:
$.ajax({
Type:"POST",
...
into :
$.ajax({
type:"POST",
...
result: