Jquery 在本地添加行后将所有数据发送到服务器时出现问题
嗨,我是jqGrid的新手。我使用的是jqGrid 4.4。我在年成功地使用了Oleg的方法 在本地添加行。我面临两个问题Jquery 在本地添加行后将所有数据发送到服务器时出现问题,jquery,jqgrid,Jquery,Jqgrid,嗨,我是jqGrid的新手。我使用的是jqGrid 4.4。我在年成功地使用了Oleg的方法 在本地添加行。我面临两个问题 未添加到末尾的行,如何才能实现这一点 我点击按钮发送所有行,如下所示: 我总是在错误块中获取警报 $("#sendButton").click(function(){ var gridData = jQuery("#list").getRowData(); var myJSONString = JSON.stringify(gridData);
$("#sendButton").click(function(){
var gridData = jQuery("#list").getRowData();
var myJSONString = JSON.stringify(gridData);
var postData = myJSONString.replace(/[\"]/g, '\"');
alert("JSON serialized jqGrid data:\n" + postData);
$.ajax({
type: "POST",
url: CONTEXT_ROOT+"/json",
data : postData,
dataType:"json",
contentType: "application/json; charset=utf-8",
success: function(response, textStatus, xhr) {
alert("success");
},
error: function(xhr, textStatus, errorThrown) {
alert("error:"+errorThrown+" textStatus : "+textStatus);
}
});
});
不确定在java控制器中保存后需要返回什么。这是控制器中的代码。我也尝试过将返回作为无效,但结果相同。还有更好的方法将来自jsp的json对象列表绑定到域对象列表。我曾经尝试过在只有一个对象的情况下进行绑定,比如使用@RequestBody的表单,如中所述
@RequestMapping(value=“/json”,method=RequestMethod.POST)
公共模型和视图saveNewCasePackOptions(@RequestBody List json){
对于(Map mJson:json){
字符串idCasePkOptions=(字符串)mJson.get(“idCasePackOptions”);
Long idcasepakoptions=(idCasePkOptions.isEmpty())?null:新的Long(idCasePkOptions);
Short-cypharecommended=新的Short((字符串)mJson.get(“cypharecommended”);
Short Distributor Approved=新的Short((字符串)mJson.get(“Distributor Approved”);
stringheightstr=(String)mJson.get(“height”);
Double height=(heightStr.isEmpty())?null:新的Double(heightStr);
字符串长度str=(String)mJson.get(“长度”);
Double-length=(lengthStr.isEmpty())?null:新的Double(lengthStr);
stringweightstr=(String)mJson.get(“高度”);
Double-weight=(weightStr.isEmpty())?null:新的Double(weightStr);
字符串宽度str=(String)mJson.get(“宽度”);
Double width=(widthStr.isEmpty())?null:新的Double(widthStr);
String stateString=(String)mJson.get(“statuscode”);
stateString=(stateString.contains(“绿色”))?“1”:“0”;
Short statuscode=新的Short(状态字符串);
CasePackOptions casePkOpt=新CasePackOptions(idCasePackOptions、cypharecommended、经销商批准、高度、长度、状态代码、重量、宽度);
System.out.println(casePkOpt);
casepackoptionservice.save(casePkOpt);
}
ModelAndView mav=新ModelAndView();
setViewName(“casepackoptions/listPage1.jsp”);
返回mav;
}
任何帮助都将不胜感激。我写下了你问题第一部分的答案 你可以从使用Spring的人那里得到问题第二部分的答案。部件
myJSONString.replace(/[\“]/g,“\”)
对我来说似乎很可疑。您可能需要使用data:{json:postData}
而不是data:postData
。您可能需要将@RequestBody
更改为@RequestParam
或@RequestParam(“json”)
。我自己不用弹簧
此外,您可以使用
$(“#list”).jqGrid(“getGridParam”,“data”)
而不是jQuery(“#list”).getRowData()
。如果您使用数据的本地分页,这将特别有用。我在您问题的第一部分写下了答案
你可以从使用Spring的人那里得到问题第二部分的答案。部件myJSONString.replace(/[\“]/g,“\”)
对我来说似乎很可疑。您可能需要使用data:{json:postData}
而不是data:postData
。您可能需要将@RequestBody
更改为@RequestParam
或@RequestParam(“json”)
。我自己不用弹簧
此外,您可以使用
$(“#list”).jqGrid(“getGridParam”,“data”)
而不是jQuery(“#list”).getRowData()
。如果您使用数据的本地分页,这将特别有用。我已经让它工作了,但不确定这是否是推荐的方法
java脚本函数是
$("#sendButton").click(function(){
var gridData = jQuery("#list").getRowData();
gridData = JSON.stringify(gridData);
alert("postData stringify data :\n" + postData);
postData = postData.replace();
$.ajax({
type: "POST",
url: CONTEXT_ROOT+"/json",
data : gridData,
contentType: "application/json; charset=utf-8",
success: function(response, textStatus, xhr) {
alert("success");
},
error: function(xhr, textStatus, errorThrown) {
alert("error:"+errorThrown+" textStatus : "+textStatus);
}
});
});
服务器代码现在是
@RequestMapping(value = "/json", method = RequestMethod.POST)
public @ResponseBody String saveNewCasePackOptions(@RequestBody List<Map> json) {
for(Map mJson : json){
String idCasePkOptions = (String)mJson.get("idCasePackOptions");
Long idCasePackOptions = (idCasePkOptions.isEmpty())?null:new Long(idCasePkOptions);
Short cypharecommended = new Short((String)mJson.get("cypharecommended"));
Short distributorapproved = new Short((String)mJson.get("distributorapproved"));
String heightStr = (String)mJson.get("height");
Double height = (heightStr.isEmpty())?null:new Double(heightStr);
String lengthStr = (String)mJson.get("length");
Double length = (lengthStr.isEmpty())?null:new Double(lengthStr);
String weightStr = (String)mJson.get("height");
Double weight = (weightStr.isEmpty())?null:new Double(weightStr);
String widthStr = (String)mJson.get("width");
Double width = (widthStr.isEmpty())?null:new Double(widthStr);
String stateString = (String)mJson.get("statuscode");
Short statuscode = new Short(stateString);
CasePackOptions casePkOpt = new CasePackOptions(idCasePackOptions, cypharecommended, distributorapproved, height, length, statuscode, weight, width);
System.out.println(casePkOpt);
casePackOptionsService.save(casePkOpt);
}
return "Success";
}
@RequestMapping(value=“/json”,method=RequestMethod.POST)
public@ResponseBody字符串saveNewCasePackOptions(@RequestBody List json){
对于(Map mJson:json){
字符串idCasePkOptions=(字符串)mJson.get(“idCasePackOptions”);
Long idcasepakoptions=(idCasePkOptions.isEmpty())?null:新的Long(idCasePkOptions);
Short-cypharecommended=新的Short((字符串)mJson.get(“cypharecommended”);
Short Distributor Approved=新的Short((字符串)mJson.get(“Distributor Approved”);
stringheightstr=(String)mJson.get(“height”);
Double height=(heightStr.isEmpty())?null:新的Double(heightStr);
字符串长度str=(String)mJson.get(“长度”);
Double-length=(lengthStr.isEmpty())?null:新的Double(lengthStr);
stringweightstr=(String)mJson.get(“高度”);
Double-weight=(weightStr.isEmpty())?null:新的Double(weightStr);
字符串宽度str=(String)mJson.get(“宽度”);
Double width=(widthStr.isEmpty())?null:新的Double(widthStr);
String stateString=(String)mJson.get(“statuscode”);
Short statuscode=新的Short(状态字符串);
CasePackOptions casePkOpt=新CasePackOptions(idCasePackOptions、cypharecommended、经销商批准、高度、长度、状态代码、重量、宽度);
System.out.println(casePkOpt);
casepackoptionservice.save(casePkOpt);
}
返回“成功”;
}
感觉需要以不同方式完成的事情
@RequestMapping(value = "/json", method = RequestMethod.POST)
public @ResponseBody String saveNewCasePackOptions(@RequestBody List<Map> json) {
for(Map mJson : json){
String idCasePkOptions = (String)mJson.get("idCasePackOptions");
Long idCasePackOptions = (idCasePkOptions.isEmpty())?null:new Long(idCasePkOptions);
Short cypharecommended = new Short((String)mJson.get("cypharecommended"));
Short distributorapproved = new Short((String)mJson.get("distributorapproved"));
String heightStr = (String)mJson.get("height");
Double height = (heightStr.isEmpty())?null:new Double(heightStr);
String lengthStr = (String)mJson.get("length");
Double length = (lengthStr.isEmpty())?null:new Double(lengthStr);
String weightStr = (String)mJson.get("height");
Double weight = (weightStr.isEmpty())?null:new Double(weightStr);
String widthStr = (String)mJson.get("width");
Double width = (widthStr.isEmpty())?null:new Double(widthStr);
String stateString = (String)mJson.get("statuscode");
Short statuscode = new Short(stateString);
CasePackOptions casePkOpt = new CasePackOptions(idCasePackOptions, cypharecommended, distributorapproved, height, length, statuscode, weight, width);
System.out.println(casePkOpt);
casePackOptionsService.save(casePkOpt);
}
return "Success";
}