jqGrid - JSON数据分配困难

时间:2014-01-29 08:49:30

标签: jquery jqgrid

我正在尝试将JSON数据分配到jqGrid。正在显示网格,但数据无法与之绑定。

我绑定jqGrid的代码是:

function fnAddDetails() {
            debugger;
            var content = {};
            content.name = $("#txtName").val();
            //content.dob = $("#txtDOB").val();
            content.group = $("#ddlGroup option:selected").text();
            content.active = "InActive";
            content.age = age;
            if ($("#chkActive").attr("checked")) {
                content.active = "Active";
            }
            var jsonText = JSON.stringify(content);
            debugger;
            $.ajax({
                type: "POST",
                url: "Test_sspl142_jQuery_JavaScript.aspx/GetDate",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: jsonText,
                success: function(data) {
                    debugger;
                    var dataget = JSON.parse(data.d);
                    var trialData = { "name": "Pratik", "group": "gold", "active": "ACTIVE", "age": "25" };
                    //var trialData = { "name": "Pratik", "group": "gold", "active": "ACTIVE", "age": "25" };

                    if (dataget != "error") {

                        $("#tblData").jqGrid({
                            datatype: 'local',
                            data: dataget,
                            colNames: ['Name', 'Group', 'Active', 'Age'],
                            colModel: [
                            { name: 'name', index: 1, width: 70 },
                            { name: 'group', index: 2, width: 90 },
                            { name: 'active', index: 3, width: 100 },
                            { name: 'age', index: 4, width: 80, align: "right" }
                            ],
                            rowNum: 10,
                            rowList: [10, 20, 30, 40],
                            viewrecords: true,
                            width: 500,
                            height: 200,
                            defaults: {
                                emptyrecords: "No records to view",
                                loadtext: "Loading..."
                            }
                        });
                    }
                    else {
                        alert("Error While Assigning to jQGrid!");
                    }
                },
                error: function(ex) {
                    alert("Error Occured while saving data in grid..." + ex);
                }
            });
        }

data $ajax()success的值为:

Object {d: "[{"name":"XYZ","group":"Gold","active":"Active","age":"0"}]"}

1 个答案:

答案 0 :(得分:0)

由于您使用json作为dataType,因此您根本不需要解析它。

另外,我怀疑这个格式不正确:

Object {d: "[{"name":"XYZ","group":"Gold","active":"Active","age":"0"}]"}

由于您将数组和元素用双引号括起来,所以它实际上是这样形成的

Object {
    d: "[{"
    name ":"
    XYZ ","
    group ":"
    Gold ","
    active ":"
    Active ","
    age ":"
    0 "}]"
};

无论哪种方式都可能无法正确解析。尝试将封闭引号更改为单引号,如下所示。

Object {d: '[{"name":"XYZ","group":"Gold","active":"Active","age":"0"}]'}