我一直在嘗試從grails控制器發送JSON響應以顯示在jQuery數據表(v 1.9)中,但我收到空響應。無法發送JSON響應從grails控制器到jQuery數據表
這裏是我的控制器: -
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;
}
這種方法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]}]
});
});
「處理」欄中的數據表顯示爲空。 誰能告訴我我在這裏錯過了什麼?任何幫助將非常感激。謝謝!
在JS控制檯應該有一些錯誤 – injecteer
我沒有在你的控制器中看到'render jsonResponse'; – dsharew
@injecteer我的JS控制檯很乾淨 –