JsTree 返回“无效的 JSON”错误

发布于 2024-09-18 13:37:41 字数 2233 浏览 8 评论 0原文

我正在尝试制作一个 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:....”,我该如何解决这个问题?


谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2024-09-25 13:37:41

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

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

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} }
            }
        },
江南烟雨〆相思醉 2024-09-25 13:37:41

我想 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);}
})

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);}
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文