![](/img/trans.png)
[英]AJAX is sending all null data to the Spring Controller using POST mapping?
[英]Basic Ajax POST sending values arrives null in the controller
我知道這是一個經常在開發人員論壇上到處問的問題,但是盡管進行了搜索,但我找不到任何問題。
我只是想通過Ajax POST將一些值傳遞給我的控制器。 Ajax接收值,但它們在我的控制器中到達null。
我嘗試聲明一個具有相同變量名的模型,並且還嘗試將每個字段放入方法的簽名中(具有相同的變量名),但結果始終相同:null。
是否有可能因為我的視圖模型與我要用來接收數據的模型不同而使它不起作用?
您是否對卡住的東西有什么想法,尤其是什么原因?
謝謝!
控制器V1
[Authorize]
[HttpPost]
public async Task<ActionResult> ShowRegistration(Models.RegisterForm rF)
{
控制器V2
[Authorize]
[HttpPost]
public async Task<ActionResult> ShowRegistration(string Id, string Status, string Checkin, string Checkout, string Cost, bool Terms, bool Info, string Member_Zkp)
{
return View();
}
模型
public class RegisterForm
{
public string Id { get; set; }
public string Status { get; set; }
public string Checkin { get; set; }
public string Checkout { get; set; }
public string Cost { get; set; }
public bool Terms { get; set; }
public int TermsINT { get; set; }
public int InfoINT { get; set; }
public bool Info { get; set; }
public string Member_Zkp { get; set; }
}
Ajax功能
function Edit() {
event.preventDefault();
var id = $('#Zkp').val();
var status = $('#StatusDrop').val();
var cost = $('#Cost').val();
var checkin = $('#Checkin').val();
var checkout = $('#Checkout').val();
var check1 = $('#check1').val();
var check2 = $('#check2').val();
var dataRegister = { "Id": id, "Status": status, "Cost": cost, "Terms": check1, "Info": check2 };
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertWait').show('fade');
setTimeout(function () {
$('#alertWait').hide('fade');
}, 4000);
$.ajax({
url: "https://localhost:44338/Registration/ShowRegistration/",
data: { "Id" : id, "Status": status, "Cost": cost, "Terms": check1, "Info": check2 },
type: 'POST',
contentType: 'application/JSON',
success: function (data) {
if (data === "success") {
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertOk').show('fade');
setTimeout(function () {
$('#alertOk').hide('fade');
}, 4000);
}
},
error: function () {
$('html, body').animate({ scrollTop: 0 }, 'fast');
$('#alertError').show('fade');
setTimeout(function () {
$('#alertError').hide('fade');
}, 4000);
}
});
}
首先
嘗試從帖子數據之類的每個鍵中刪除雙引號。
data: { Id : id, Status: status, Cost: cost, Terms: check1, Info: check2 }
接着,
嘗試從ajax調用中隱藏此行。
contentType: 'application/JSON',
發送對象之前將其序列化為json:
data: JSON.stringify({ "Id": id, "Status": status, "Cost": cost, "Terms": check1, "Info": check2 }),
您也可以接收模型,而不用聲明x變量
public async Task<ActionResult> Index(RegisterForm model)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.