2014-12-24 68 views
1

我一直在嘗試從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]}] 
    }); 
}); 

「處理」欄中的數據表顯示爲空。 誰能告訴我我在這裏錯過了什麼?任何幫助將非常感激。謝謝!

+0

在JS控制檯應該有一些錯誤 – injecteer

+0

我沒有在你的控制器中看到'render jsonResponse'; – dsharew

+0

@injecteer我的JS控制檯很乾淨 –

回答

0

您的操作對您創建的JSON對象不起任何作用。無論是使用render data或返回的JSON對象作爲操作的返回值:

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

而且您的服務方法看起來Java的雜交。使用JSON生成器會降低50%的複雜度

+0

對不起,我是新來的Grails。我用Java編寫了相同的代碼,並通過Spring控制器發送了響應,並且它工作正常。但是我無法使用Grails發送響應。 –

+0

請參閱更新... – injecteer

+0

我使用了render jsonResponse,它給了我下面的JS錯誤: - Datatables警告:無法解析來自服務器的JSON數據。這是由JSON格式錯誤引起的。我也使用JSONResponse呈現爲JSON,但它然後拋出錯誤java.lang.UnsupportedOperationException:JsonObject 您能否幫我解決這個問題! –

相關問題