gpt4 book ai didi

javascript - Controller 操作接收旧值作为 AJAX 调用的参数

转载 作者:行者123 更新时间:2023-12-03 01:41:47 24 4
gpt4 key购买 nike

我使用简单的 jquery 向 ASP.NET 中的 MVC Action 发送 Ajax 请求,第一次工作正常,但在第一次调用之后,所有调用都倾向于接收 Action 上第一个发布的值。

当我尝试调试 JS 代码时,新值会填充在那里,但在操作上,它们会变回第一个值。

我的 Jquery 代码:

  $('.send-quote').click(function (e) {
var $form = $('#manualBooking');

var formData = $('#manualBooking').serializeArray();

var name = formData[0].value;
var contact = formData[1].value;
var email = formData[2].value;
var bookingDate = formData[3].value;
var pickupLocation = formData[4].value;
var dropOffLocation = formData[5].value;
var flight = formData[6].value;
var passenger = formData[7].value;
var luggage = formData[8].value;
var message = formData[9].value;
var language = formData[10].value;
var hours = formData[11].value;
var days = formData[12].value;
var radio = formData[13].value;


if (radio == 'yes') {

$form.validate({
errorClass: 'customErrorClass',
rules: {
name: {
required: true
},
phone: {
required: true
},
email: {
required: true,
email: true
},
depdate: {
required: true
},
txtAddresspickupFromAirport: {
required: true
},
txtAddressdropOffFromAirport: {
required: true
},
flight: {
required: true
},
passenger: {
required: true
},
luggage: {
required: true
},
message: {
required: true
},
lang: {
required: true
},
hours: {
required: true
},
days: {
required: true
}
},
messages: {
email: "*Please specify a valid email address"
},
submitHandler: function (form) {
console.log(' NOT OK')
$.ajax({
type: "POST",
url: '/Home/InsertManualBookingForm',
data: {
Name: name,
ContactNo: contact,
Email: email,
BookingDateTime: bookingDate,
PickUpLocation: pickupLocation,
DropOffLocation: dropOffLocation,
FlightNumber: flight,
Passenger: passenger,
Luggage: luggage,
Message: message,
Language: lang,
HoursNeeded: hours,
DaysNeeded: days
},
success: function (data) {
swal("an email will be sent to you shortly", {
icon: "success",
});

// document.getElementById("manualBooking").reset();


}
});

}
});
}

和我的 MVC 操作代码:

[OutputCache(Duration = 0, VaryByParam = "none")]
public JsonResult InsertManualBookingForm(ManualBookingForm manualBookingForm)
{

this.ModelState.Clear();
ModelState.Clear(); // force to use new model values

String Result = String.Empty;
this.ModelState.Clear();
ModelState.Clear(); // force to use new model values


try
{
string agentCardsSerialize = JsonConvert.SerializeObject(manualBookingForm);

this.ModelState.Clear();
ModelState.Clear(); // force to use new model values



Result = Common.Common.ApiCall(Common.Common.APIEndpoint + "Home/InsertManualBookingForm", agentCardsSerialize);//Common.Common.ApiCall(URL_Request, commisionType,token);
return Json(Result, JsonRequestBehavior.AllowGet);

}
catch (Exception ex)
{

throw;
}
}

如您所见,我什至尝试了清空缓存和清除模型状态的在线解决方案,但似乎没有解决方案有效。

我的 AJAX 调用或 MVC 操作有问题吗?请指教。

最佳答案

尝试在ajax调用中添加cache:false

  $.ajax({
type: "POST",
url: '/Home/InsertManualBookingForm',
cache:false,
data: {
Name: name,
ContactNo: contact,
Email: email,
BookingDateTime: bookingDate,
PickUpLocation: pickupLocation,
DropOffLocation: dropOffLocation,
FlightNumber: flight,
Passenger: passenger,
Luggage: luggage,
Message: message,
Language: lang,
HoursNeeded: hours,
DaysNeeded: days
},
success: function (data) {
swal("an email will be sent to you shortly", {
icon: "success",
});

// document.getElementById("manualBooking").reset();


}
});

让我们知道它是否有效

PS :在 ajax 调用之前在 submit handler 中获取表单的值(Name、ContactNo、Email ...等),可能就是这个原因始终从第一次调用中获取值。

关于javascript - Controller 操作接收旧值作为 AJAX 调用的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50812080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com