JQGrid中未显示JSON数据

本文关键字:JSON 数据 显示 JQGrid | 更新日期: 2023-09-27 18:25:41

我看过所有相关答案中的建议,但都无济于事。我尝试了各种选择,但又一次都无济于事。我使用ASP.NET,C#作为处理语言,Oracle 10g用作数据库引擎。

这是我的.aspx文件:

<form id="LocalizationSearch" name="locSearch" defaultbutton="updateButton" runat="server">
   <script language="javascript" type="text/javascript">
    jQuery(document).ready(function () {
        var createLanguageList = function () {
            $("#list").jqGrid({
                url: "KPHandler.ashx?langname=English",
                datatype: "json",
                contentType: "application/json; charset=utf-8", 
                mtype: 'POST',
                //datatype: "local",
                //data: mydata,
                autowidth: true,
                height: 150,
                colNames: ['DelLanguageID', 'DocRecID', 'LanguageID', 'DocPartNumber,', 'NewLanguage', 'ConcentraIDLang', 'ActFromTranslation'],
                colModel: [
                    { name: 'DelLanguageID', index: 'DelLanguageID', width: 65, editable: false },
                    { name: 'DocRecID', index: 'DocRecID', width: 65, editable: false },
                    { name: 'LanguageID', index: 'LanguageID', width: 65, editable: false },
                    { name: 'DocPartNmbr', index: 'DocPartNmbr', width: 65, editable: true },
                    { name: 'NewLanguage', index: 'NewLanguage', editable: true, edittype: 'checkbox', editoptions: { value: "1:0"} },
                    { name: 'ConcentraIDLang', index: 'ConcentraIDLang', width: 65, editable: true },
                    { name: 'ActFromTranslation', index: 'ActFromTranslation', width: 120, editable: true }
                ],
                rowNum: 5,
                rowList: [10, 20, 30],
                pager: '#pager',
                sortname: 'DelLanguageID',
                viewrecords: true,
                //altRows: true,
                //sortorder: "desc",
                caption: "Deliverable Languages",
                //height: "100%",
                loadonce: true, 
                gridview: true, 
                autoencode: true,
                loadError: function (jqXHR, textStatus, errorThrown) {
                    alert('Error: ' + textStatus + errorThrown + jqXHR.responseText);
                }
            });  // JQGrid
        };  // Create function
        $("#btnLanguages").click(function () {
            createLanguageList();
        });
    });   // document ready 
</script>
    <table cellspacing="1" cellpadding="0" width="100%">
        <tr>
            <td valign="top">
                <table id="list"></table>
                <div id="pager"></div>
        </td>
        </tr>
    </table>
    <input type="button" id="btnLanguages" value="Work With Languages"  />
</form>

这是我构建JSON对象的C#代码:

<%@ WebHandler Language="C#" Class="KPHandler" %>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Configuration; 
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OracleClient;
public class KPHandler : IHttpHandler
{
    private DKCL.ReportDK Rpt = new DKCL.ReportDK();
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        System.Collections.Specialized.NameValueCollection forms = context.Request.Form;
        string strOperation = forms.Get("oper");
        switch (strOperation)
        {
            case null:  // means this is the first time load
                string strSQL = "SELECT DelLanguageID, DocRecID, LanguageID, DocPartNmbr, NewLanguage, " +
                                "ConcentraIDLang, ActFromTranslation FROM DELLANGUAGES WHERE DOCRECID = 18787";
                DataTable dlLanguages = Rpt.GetDataTable(strSQL);
                context.Response.Write(GetJson(dlLanguages));
                break;
            default:
                break;
        }  // End Switch
    }
    // This is generic function that converts DataTable into JSON object.
    public string GetJson(DataTable dt)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new
        System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows =
          new List<Dictionary<string, object>>();
        Dictionary<string, object> row = null;
        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName.Trim(), dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

这是上面代码返回的JSON数据(我实际上得到了Fiddler2所示的数据),以及我自己对代码的调试

            [{"DELLANGUAGEID":"4613","DOCRECID":"18787","LANGUAGEID":"12","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02609839","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4614","DOCRECID":"18787","LANGUAGEID":"34","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4615","DOCRECID":"18787","LANGUAGEID":"1","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606925","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4616","DOCRECID":"18787","LANGUAGEID":"13","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02609850","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4617","DOCRECID":"18787","LANGUAGEID":"11","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606924","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4618","DOCRECID":"18787","LANGUAGEID":"3","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606920","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4619","DOCRECID":"18787","LANGUAGEID":"21","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"39703","DOCRECID":"18787","LANGUAGEID":"142","DOCPARTNMBR":"593957-001","NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"89446","DOCRECID":"18787","LANGUAGEID":"161","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"97174","DOCRECID":"18787","LANGUAGEID":"162","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"112630","DOCRECID":"18787","LANGUAGEID":"164","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"104902","DOCRECID":"18787","LANGUAGEID":"163","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null}]

知道可能出了什么问题吗网格显示正确数量的记录以及寻呼机中的页面任何帮助都将不胜感激
顺便说一句,我尝试了GET与POST、json与jsonstring等以及许多其他选项,包括json阅读器,但没有成功。

JQGrid中未显示JSON数据

colModel索引属性旨在精确反映JSON结果中的键。请参阅文档:

index:传递给要对数据进行排序的服务器的名称(请注意,我们可以传递列号)。通常,这是数据库中的一个或多个名称——这是服务器端排序,因此传递的内容取决于服务器期望接收的内容。

考虑到这些信息,示例中的索引的大小写似乎不正确。请尝试将它们更正为正确的大小写或使用数值。

示例:

colModel: [{ name: 'DelLanguageID', index: 'DELLANGUAGEID', width: 65, editable: false },...

谢谢Strake。这对我来说很痛苦,但很有教育意义。现在只需更改**ColModel**就可以了。再次感谢。