开发者

JsTree returns "invalid JSON" error

I'm trying to make a Jstree, the code for it is the following:

$(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" ] 
        }
    });

});

This is the Json I get back from the Ajax call

[
{
    "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"
        }
    ]
}
] 


I get an Error saying 'Invalid JSON:....', how do I solve this?

thanks.


The JSON returned is valid. If i were to take a guess - maybe you need to specify the type. EG:

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


I guess json_data rather needs to be an jQuery.ajax call:

"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);}
})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜