AJAX POST in ASP.NET Core Razor page will not work. It always returns a 400 Bad Request.
I have the following page method code :
[ValidateAntiForgeryToken]
public async Task<IActionResult> OnPostProcessCCPaymentAsync(CheckInPaymentModel checkInPaymentModel)
{
return new JsonResult(checkInPaymentModel.AmountExtra);
}
The following is set in the page :
@Html.AntiForgeryToken()
And the following JS AJAX call :
$.ajax({
type: "POST",
url: "/CheckIn/Payment?handler=ProcessCCPayment",
contentType: "application/json",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify({
// Those property names must match the property names of your PromotionDecision view model
Donate: true
}),
success: function (response) {
$(".paymentDetails .loading").addClass("loader").removeClass("loading");
},
failure: function (response) {
$(".paymentDetails .loading").addClass("loader").removeClass("loading");
}
});
If the Ajax Type is changed to GET & the method changed to OnGetProcessCCPaymentAsync then it correctly sends to the server.
However the AJAX POST in Core Razor always fails with a 400 Bad Request.
I am debugging directly from Visual Studio so the URL is http://localhost:62632/CheckIn/Payment so I don't know how to locate the logs to see what error is occuring when the debug instance is receiving the request.
Any ideas or advice would be greatly appreciated.