我一直在尝试从grails控制器发送JSON响应以显示在jQuery datatable(v 1.9)中,但是我得到的响应为空。
这是我的控制器:
def breakHierarchy =
{
logger.info("breakHierarchy begins here");
userObject = session.myBeanU;
userLoggedIn = userStatus.checkUserLoggedIn(userObject);
ArrayList<HierarchyBreakBean> mappingList;
JsonObject jsonResponse;
if(userLoggedIn) {
def hierarchyBreakInstance = new HierarchyBreak(params);
String[] divisionList = params.searsDivNumber;
String[] lineList = params.searsLineNumber;
String[] sublineList = params.searsSubLineNumber;
String[] classList = params.searsClass;
try {
mappingList = hierarchyBreakService.searchHierarchy(divisionList, lineList, sublineList, classList);
jsonResponse = hierarchyBreakService.loadData(request, response, mappingList);
} catch(Exception ex){
logger.info("Exception in breakHierarchy: ");
ex.printStackTrace();
hierarchyBreakInstance.errors.reject(message(code: ex.toString()));
render(view: "hierarchyBreak", model: [hierarchyBreakInstance: hierarchyBreakInstance]);
return;
}
} else {
redirect(action: loginError);
}
return;
}
此方法在Service中的loadData()方法:
try {
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("sEcho", sEcho);
jsonResponse.addProperty("iTotalRecords", iTotalRecords);
jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
Iterator<HierarchyBreakBean> it = list.iterator();
while(it.hasNext()){
JsonArray row = new JsonArray();
record = it.next();
row.add(new JsonPrimitive("<input type='checkbox' class='singleSelect' id='item' name='item' value='' />"));
row.add(new JsonPrimitive(record.getSearsCategoryNumber()));
row.add(new JsonPrimitive(record.getSearsBusNumber()));
row.add(new JsonPrimitive(record.getSearsDivNumber()));
row.add(new JsonPrimitive(record.getSearsLineNumber()));
row.add(new JsonPrimitive(record.getSearsSublineNumber()));
row.add(new JsonPrimitive(record.getSearsClassNumber()));
row.add(new JsonPrimitive(record.getKmartDivNumber()));
row.add(new JsonPrimitive(record.getKmartDeptNumber()));
row.add(new JsonPrimitive(record.getKmartCatGroupNumber()));
row.add(new JsonPrimitive(record.getKmartCatgNumber()));
row.add(new JsonPrimitive(record.getKmartSubCatgNumber()));
row.add(new JsonPrimitive(record.getOrderSystem()));
row.add(new JsonPrimitive(record.getSearskmartOrderSystemCode()));
data.add(row);
}
jsonResponse.add("aaData", data);
response.setContentType("application/Json");
response.getWriter().print(jsonResponse);
return jsonResponse;
} catch (Exception e) {
e.printStackTrace();
}
这是我正在使用的JS:-
var myTable = null;
$("#searchButton").click(function(){
var url = appcontextPath + "/login/breakHierarchy";
$("#row_header").show();
myTable = $("#mappingTable").dataTable({"bServerSide": true,
"bProcessing": true,
"sAjaxSource": url,
"sPaginationType": "full_numbers",
"bJQueryUI": true,
"bDestroy": true,
"aaSorting": [[4,'asc']],
"bFilter": false,
"aoColumnDefs": [{"sWidth": "20%", "aTargets": [1,2,3,4,5], "sClass": "center"},
{"bSortable": false, "aTargets": [0]}]
});
});
数据表的“处理中”栏显示为空。 谁能告诉我我在这里想念什么吗? 任何帮助将非常感激。 谢谢!
您的操作对您创建的JSON对象没有任何作用。 使用render data
或返回json对象作为操作的返回值:
mappingList = hierarchyBreakService.searchHierarchy(divisionList, lineList, sublineList, classList);
jsonResponse = hierarchyBreakService.loadData(request, response, mappingList);
render jsonResponse
同样,您的服务方法看起来也像Java。 使用JSON Builder可以将复杂度降低50%