使用php为jstree生成备用JSON

使用php为jstree生成备用JSON,php,jquery,ajax,json,jstree,Php,Jquery,Ajax,Json,Jstree,我试图在PHP控制器中为jstree生成备用JSON代码 我正在创建看起来正确的数据,但是jstree没有显示它 我的javascript如下所示: $this->registerJs(" $(function() { $('#statustree').jstree({ 'core' : { 'data' : {

我试图在PHP控制器中为jstree生成备用JSON代码

我正在创建看起来正确的数据,但是jstree没有显示它

我的javascript如下所示:

$this->registerJs("
    $(function() {

        $('#statustree').jstree({ 
            'core' : 
            {
                'data' : 
                {
                    'datatype' : 'json',
                    'url' : '/myaccount/buildstatustree',
                }
            }
        });

        $('#statustree').on('loaded.jstree', function() 
        {
            $('#statustree').jstree('open_all');
        });
    })
", \yii\web\VIEW::POS_READY);
    // convert to JSON format for jstree

    $tree = array();

    $parent = new stdClass();
    $parent->id = 'P1';
    $parent->parent = '#';
    $parent->text = $username;
    $tree[] = $parent;

    $student1 = new stdClass();
    $student1->id = 'S1';
    $student1->parent = 'P1';
    $student1->text = 'Poly';
    $tree[] = $student1;

    $student1 = new stdClass();
    $student1->id = 'S2';
    $student1->parent = 'P1';
    $student1->text = 'Bob';
    $tree[] = $student1;

    // convert to json and send
    header('Content-type: application/json');
    return json_encode( $tree );
[
    {"id":"P1","parent":"#","text":"user2"},
    {"id":"S1","parent":"P1","text":"Poly"},
    {"id":"S2","parent":"P1","text":"Bob"}
]
我的php如下所示:

$this->registerJs("
    $(function() {

        $('#statustree').jstree({ 
            'core' : 
            {
                'data' : 
                {
                    'datatype' : 'json',
                    'url' : '/myaccount/buildstatustree',
                }
            }
        });

        $('#statustree').on('loaded.jstree', function() 
        {
            $('#statustree').jstree('open_all');
        });
    })
", \yii\web\VIEW::POS_READY);
    // convert to JSON format for jstree

    $tree = array();

    $parent = new stdClass();
    $parent->id = 'P1';
    $parent->parent = '#';
    $parent->text = $username;
    $tree[] = $parent;

    $student1 = new stdClass();
    $student1->id = 'S1';
    $student1->parent = 'P1';
    $student1->text = 'Poly';
    $tree[] = $student1;

    $student1 = new stdClass();
    $student1->id = 'S2';
    $student1->parent = 'P1';
    $student1->text = 'Bob';
    $tree[] = $student1;

    // convert to json and send
    header('Content-type: application/json');
    return json_encode( $tree );
[
    {"id":"P1","parent":"#","text":"user2"},
    {"id":"S1","parent":"P1","text":"Poly"},
    {"id":"S2","parent":"P1","text":"Bob"}
]
我的控制器正在被调用并返回如下字符串:

$this->registerJs("
    $(function() {

        $('#statustree').jstree({ 
            'core' : 
            {
                'data' : 
                {
                    'datatype' : 'json',
                    'url' : '/myaccount/buildstatustree',
                }
            }
        });

        $('#statustree').on('loaded.jstree', function() 
        {
            $('#statustree').jstree('open_all');
        });
    })
", \yii\web\VIEW::POS_READY);
    // convert to JSON format for jstree

    $tree = array();

    $parent = new stdClass();
    $parent->id = 'P1';
    $parent->parent = '#';
    $parent->text = $username;
    $tree[] = $parent;

    $student1 = new stdClass();
    $student1->id = 'S1';
    $student1->parent = 'P1';
    $student1->text = 'Poly';
    $tree[] = $student1;

    $student1 = new stdClass();
    $student1->id = 'S2';
    $student1->parent = 'P1';
    $student1->text = 'Bob';
    $tree[] = $student1;

    // convert to json and send
    header('Content-type: application/json');
    return json_encode( $tree );
[
    {"id":"P1","parent":"#","text":"user2"},
    {"id":"S1","parent":"P1","text":"Poly"},
    {"id":"S2","parent":"P1","text":"Bob"}
]
调用时微调器旋转,但微调器消失,并且“我的树”不显示

我怀疑我没有正确地生成备用JSON响应,但我尝试的任何东西都不起作用

谢谢
-John

您正在生成的数据看起来很好(前提是您在开发工具中看到的对jsTree发出的AJAX调用的响应)

您可能需要检查所有的头文件是否都正常-它是否真的作为JSON提供?您还可以尝试添加字符集,以防万一:

header('Content-Type: application/json; charset=UTF-8');
我看到您已经在尝试强制jQuery将响应视为JSON,而不考虑标题,但是使用
“dataType”
而不是
“dataType”


如果这不起作用-请分享您在开发人员控制台的网络面板中看到的内容-jsTree发出的AJAX调用的标题和响应。

JSON.stringify({'a':'foo'})==JSON格式的对象当您对数组进行JSON编码时,您可以执行类似于$tree[0]=new array()而不是$tree[0][0]=new student的操作