我的應用程式中有一個簡單的 jQuery $.post 問題。我在互聯網上嘗試了很多方法和解決方案,但無濟于事。發送發布請求時,我看到 400 錯誤
public class RecoveryAdhocInvoicingModel : PageModel
{
Public IActionResult OnPostDoJob(string invNo, string account)
{
//var emailAddress = Request.Form["emailaddress"];
// do something with emailAddress
return new JsonResult("");
}
}
在我的腳本中,$.post 方法是通過單擊按鈕呼叫的:
$('#submitBtnUpdateJob').click(function (evt) {
var formdata = new FormData();
formdata.append("invNo", $('#adhocInvoiceNumber').val());
formdata.append("account", $('#invoiceAccountInput').val());
$.post(window.location.href '?handler=DoJob', JSON.stringify(formdata), function () {
alert('Posted');
});
});
解決方案檔案夾路徑
uj5u.com熱心網友回復:
您需要將RequestVerificationToken標頭添加到請求中。
$.ajax({
type: "POST",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
url: window.location.href '?handler=DoJob',
data: {
"invNo": $('#adhocInvoiceNumber').val(),
"account": $('#invoiceAccountInput').val()
},
contentType: "application/x-www-form-urlencoded"
}).done(function (response) {
alert('Posted');
});
檔案:https ://www.learnrazorpages.com/security/request-verification#ajax-post-requests-and-json
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511395.html
