![](/img/trans.png)
[英]What is wrong with this JavaScript (using Ajax call in MVC 3)?
[英]What is wrong with this Ajax call?
I'm trying to generate the qr code and pop up it in mvc. Below is the code
$("#generateQRCode").on('click', function (e) {
//TODO prevent deafualt
var Details =
{
cityId: $('#city_Id').val(),
cityTypeId: $('#Type_Id').val(),
busId: $('bus_Id').val(),
serialId: $('#serial_Id').val(),
makeId: $('#make_Id').val()
}
$.ajax({
traditional: true,
async: true,
type: 'GET',
// headers: { 'X-CSRF-TOKEN': $("input[name='__RequestVerificationToken']").val() },
url: "/Home/Index/",
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(Details),
//processData: false,
//cache: false,
success: function (data) {
if (data.result === true)
{
debugger;
$("#afterActionConfirmationModal").modal('show');
}
else {
debugger;
alert("error1");
}
},
error:
alert("error2"),
});
//controller
[HttpGet]
public IActionResult Index(object details)
{
}
我在调试时总是收到error2警报代码块,然后转到操作方法,对象“详细信息”值填充为0。没有错误消息显示。 请帮帮我
我总是收到error2警报
因为这没有做您认为做的事:
error: alert("error2")
这不会将alert()
设置为error
回调处理程序。 这将立即执行alert()
并将其结果 ( undefined
)设置为error
回调处理程序。 因此, alert()
将显示是否存在错误,因为它将在AJAX调用甚至执行之前显示。
像success
回调处理程序一样,将其包装在用作回调的函数中:
error: function () {
alert("error2");
}
更新:从下面的注释中,您似乎还期望C#中的object
类型很多。 该类型没有有用的属性,因此无处可找到要发布到服务器的值。 使用自定义类型:
public class Details
{
public int cityId { get; set; }
public int cityTypeId { get; set; }
public int busId { get; set; }
public int serialId { get; set; }
public int makeId { get; set; }
}
并使用该类:
public IActionResult Index(Details details)
您可能还需要仅使用Details
替换JSON.stringify(Details)
,因为您可能想发送对象本身而不是序列化的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.