无法将Grails控制器的JSON响应发送到jQuery数据表

时间:2014-12-24 11:51:02

标签: json grails jquery-datatables

我一直在尝试从grails控制器发送JSON响应以在jQuery datatable(v 1.9)中显示,但我得到了null响应。

这是我的控制器: -

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]}]
    });
});

数据表显示为空,其中包含&#34;处理&#34;酒吧。 有谁能告诉我,我在这里失踪了什么?任何帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您的操作对您创建的JSON对象不执行任何操作。使用render data或返回json对象作为操作的返回值:

 mappingList = hierarchyBreakService.searchHierarchy(divisionList, lineList, sublineList, classList);
 jsonResponse = hierarchyBreakService.loadData(request, response, mappingList);
 render jsonResponse

此外,您的服务方法也适用于java-ish。使用JSON Builder可将复杂性降低50%