[英]How to Pass dynamic ceated table row column Data values from JSP to Spring controller through ajax call
[英]Data pass from AJAX call to Controller but getting null values
From below code i want to send data to my controller but when i add debug point to my controller function then receiving all the values null. 我不替换数据类型是 json 的文本。
jQuery.ajax({
url: "../VisitorEntry/SaveVisitorEntry",
type: "POST",
//data: "{visitpurpose:'" + visitpurpose + "'}",
data: {
visitpurpose: visitpurpose,
txt_visitor_name: txt_visitor_name,
txt_company_name: txt_company_name,
txt_emp_name: txt_emp_name,
txt_phone: txt_phone,
txt_designation: txt_designation,
txt_address: txt_address,
txt_country_name: txt_country_name,
txt_state_name: txt_state_name,
txt_city_name: txt_city_name
},
dataType: "text",
traditional: true,
//contentType: "application/json; charset=utf-8",
processData: false,
success: function (response) {
debugger
jQuery("#wait").css("display", "none");
QrCodeImage.setAttribute('src', "data:image/jpg;base64," + response);
},
error: function (response) {
debugger
jQuery("#wait").css("display", "none");
console.log(response.responseText);
}
});
下面是我在 controller 上的 function ,我想接收所有值,但接收 null。
[HttpPost]
public ActionResult SaveVisitorEntry(string visitpurpose, string txt_visitor_name, string txt_company_name, string txt_emp_name, string txt_phone,
string txt_designation, string txt_address, string txt_country_name, string txt_state_name, string txt_city_name, string txt_emp_number)
{
VisitorEntryDA pdm = new VisitorEntryDA();
DataSet dataSet = new DataSet();
Bitmap qrCodeImage = null;
dataSet = pdm.visitorEntry(visitpurpose, txt_visitor_name, txt_company_name, txt_emp_name, txt_phone,
txt_designation, txt_address, txt_country_name, txt_state_name, txt_city_name, txt_emp_number);
for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
{
string iserror = dataSet.Tables[0].Rows[i]["iserror"].ToString();
string message = dataSet.Tables[0].Rows[i]["message"].ToString();
string id = dataSet.Tables[0].Rows[i]["visitor_id"].ToString();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
//var data = { "visitpurpose":visitpurpose};
QRCodeData qrCodeData = qrGenerator.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
qrCodeImage = qrCode.GetGraphic(20);
ImageConverter converter = new ImageConverter();
}
return Content(Convert.ToBase64String(BitmapToBytes(qrCodeImage)));
}
一方面,通过将 c# 中的参数列表更改为单个 class (在我的示例中,DTO 仅表示数据传输对象),让您的生活更轻松:
public class VisitorDto
{
public string visitpurpose {get;set;}
public string txt_visitor_name {get;set;}
public string txt_company_name {get;set;}
public string txt_emp_name {get;set;}
public string txt_phone {get;set;}
public string txt_designation {get;set;}
public string txt_address {get;set;}
public string txt_country_name {get;set;}
public string txt_state_name {get;set;}
public string txt_city_name {get;set;}
public string txt_emp_number {get;set;}
}
并在您的方法中使用,例如:
public ActionResult SaveVisitorEntry(VisitorDTO model){
...
}
然后你也可以删除 txt_ 的前缀,因为它有点麻烦,但我会留给你。
在那之后它可能会起作用。
由于在您的 Ajax class 中,您的Data:
是 object ,因为您已将其声明为Data: {}
。
{} - 是 javascript 中的 object 语法。 到达您的 controller 时它是 null 的原因很可能是因为您发送了 object 但 Z594C18AB03F2C6E04C 期待字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.