try 使用AJAX调用对.Net核心身份注册页面上输入的值进行一些验证.
这是我的代码
注册器.cshtml
按钮代码-
<button type="button" class="btn btn-primary" id="Submit" >Create Account</button>
呼叫码-
$("#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));
}
});
});
Urlval=‘http://localhost:53196/Identity/Account/Register?handler=CheckvcNumber’
In 注册器.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 注册器.cshtml.cs
希望有人能帮忙!
谢谢