1. 程式人生 > >jqGrid 能獲取json 資料,前端資料顯示不出!(解決)

jqGrid 能獲取json 資料,前端資料顯示不出!(解決)

jqGrid 從後臺能獲取json 資料,但是前臺grid資料顯示不出!(最後朋友幫忙解決的)
這裡寫圖片描述

一、首先要確認json格式要正確(是否拼接錯誤了)

格式不清楚的可以去百度一下。

二、錯誤處理

jQuery("#_lessonGrid").jqGrid({
                url: jQuery.getBasePath() + '/command/TestAction?flag=getStudentNameIsNotNull',
                datatype: "json",
                colNames:['ID', '姓名'
,'性別', '年齡'], colModel:[ {name:'ID',index:'ID', width:55, align:'center',hidden:true,key:true}, {name:'NAME',index:'NAME', width:55, align:'center'}, {name:'SEX', index:'SEX', align:'center',width:175}, {name:'AGE'
, index:'AGE', align:'center',width:175} ], width:_width_ -201, height:240, rowNum:10, rowList:[10,20,30], pager: '#_lessonBar', sortname: 'ID', viewrecords: true, sortorder: "desc"
, multiselect: true, jsonReader: { repeatitems : false }, rownumbers : true, caption: "學員管理", afterInsertRow : function(rowid, data) { ... } }); jQuery("#_lessonGrid").jqGrid('navGrid','#_lessonBar',{add : false, del : false, edit : false, refresh : true, search : false}); });

上面的程式碼問題出在這個地方,jqGrid獲取的json串要相互匹配

區分大小寫!!!!

這裡寫圖片描述

都改為小寫,name值要和json資料相匹配,問題解決。

json相關問題1

這裡寫圖片描述
這裡寫圖片描述

前臺獲取:
這裡寫圖片描述

資料庫中有欄位,但是後臺穿過來的json是沒有class這個欄位

問題解決:可能是關鍵字的問題,我把資料庫中的欄位名改為class,前臺也改為sclass,問題就解決了。
這裡寫圖片描述

json相關問題2

這裡寫圖片描述

這裡寫圖片描述
後臺傳回json,但是獲取不到

後臺程式碼:

public boolean execute(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        this.request = request;
        this.response = response;
        modulePath = "view/manager/test"; // 此引數必須填寫 根據此路徑找到ftl檔案
        String flag = request.getParameter("flag");
        TestImpl ti = new TestImpl();
        JSONObject json = new JSONObject();
        String info = "";
        response.setCharacterEncoding("UTF-8");
        ....
        if("getstudentshow".equalsIgnoreCase(flag)){
            ...
            info="";
            List list = ti.getStudentShow(request);
            json.put("rows", list);
            returnValueHandle(response,true,json,info);
            ...
        }
        return false;
    }
}

這裡是將json資料放入list裡然後put在json裡的,最後傳回前臺,和json資料傳回是不同的,不能直接獲取,需要遍歷


問題解決。

json相關問題3

這裡寫圖片描述
問題:做更新資料,但是傳過去的id,獲取不到,其他的值卻是可以獲取。
解決:把id放在url後連結傳遞,值可以傳過去