JsTree返回“无效的JSON”错误

时间:2010-09-07 14:13:55

标签: jquery json jstree

我正在尝试制作一个Jstree,其代码如下:

$(function () {
$("#groups")
    .jstree({ 
        "plugins" : [ "themes", "json_data", "ui", "crrm", "cookies", "dnd", "search", "types", "contextmenu" ],

        "json_data" : {
            "ajax" : {
                "url" : base_url+"/json",
                "data" : function (n) {
                    return { id : n.attr ? n.attr("id") : 0 };
                },
                error: function(e,b,d){alert(d);}

            }
        },

        "types" : {
            "max_depth" : -2,
            "max_children" : -2,
            "valid_children" : [ "drive" ],
            "types" : {
                "default" : {
                    "valid_children" : "none",
                    "icon" : {
                        "image" : "./file.png"
                    }
                },
                "folder" : {
                    "valid_children" : [ "default", "folder" ],
                    "icon" : {
                        "image" : "./folder.png"
                    }
                }

            }
        },

        "ui" : {
            "initially_select" : [ "node_4" ]
        },
        "core" : { 
            "initially_open" : [ "node_2" , "node_3" ] 
        }
    });

});




这是我从Ajax调用中回来的Json

[
{
    "data" : "Genral ",
    "attr" : {
        "id" : "8"
    },
    "state" : "open",
    "children" : [
        {
            "data" : "onec ",
            "attr" : {
                "id" : "16"
            },
            "state" : "close"
        },
        {
            "data" : "onec2",
            "attr" : {
                "id" : "21"
            },
            "state" : "close"
        }
    ]
},
{
    "data" : "Stuff ",
    "attr" : {
        "id" : "9"
    },
    "state" : "open",
    "children" : [
        {
            "data" : "one9 ",
            "attr" : {
                "id" : "23"
            },
            "state" : "close"
        },
        {
            "data" : "bababa ",
            "attr" : {
                "id" : "24"
            },
            "state" : "close"
        }
    ]
}
] 



我得到一个错误,说'无效的JSON:....',我该如何解决这个问题?


感谢。

2 个答案:

答案 0 :(得分:3)

返回的JSON有效。如果我猜测 - 也许你需要指定类型。 EG:

 $("#groups").jstree({
        "json_data": {
            "ajax": {
                "type": "POST",
                "url": base_url+"/json",
                "data": function (n) { return { id: n.attr ? n.attr("id") : 0} }
            }
        },

答案 1 :(得分:0)

我猜 json_data 需要进行jQuery.ajax调用:

"json_data" : $.ajax({
    "dataType": "json",
    "url" : base_url+"/json",
    "data" : function (n) {
        return { id : n.attr ? n.attr("id") : 0 };
    },
    error: function(e,b,d){alert(d);}
})