Extjs-包含来自php文件的数据的组合框不工作

Extjs-包含来自php文件的数据的组合框不工作,extjs,combobox,extjs4,extjs4.1,Extjs,Combobox,Extjs4,Extjs4.1,我有一个组合框,我想从php服务器获取数据 这是我的Index.html代码 这是我的data.php文件。 但是,当我点击组合,然后什么也没有显示:(。我怎么才能解决这个问题,谢谢 <?php // function to create json function GetCategories() { $categories = "{'rows':["; $categories = "{'id':'1', 'name':'Category 1'},"; $categ

我有一个组合框,我想从php服务器获取数据
这是我的Index.html代码

这是我的data.php文件。
但是,当我点击组合,然后什么也没有显示:(。我怎么才能解决这个问题,谢谢

<?php
// function to create json
function GetCategories() {
    $categories = "{'rows':[";
    $categories = "{'id':'1', 'name':'Category 1'},";
    $categories .= "{'id':'2', 'name':'Category 2'},";
    $categories .= "{'id':'3', 'name':'Category 3'}";
    $categories .= "]}";
    return $categories;
}
echo GetCategories(); // sent to client
?>

主要问题在于您的存储定义。您正在使用ExtJS 3的定义。这是在ExtJS 4.1中定义存储的正确方法:

        var values = new Ext.data.Store({
            fields: [
                {name: 'id', type: 'integer'},
                {name: 'name', type: 'string'}
            ],
            proxy: new Ext.data.HttpProxy({
                url: 'data.php',
                reader: {
                    type: 'json',
                    root: 'rows'
                }
            }),
            autoLoad: true
        });
您的
json数据也有问题(第2行中也缺少一个句点)。这将起作用:

// function to create json
function GetCategories() {
    $categories = '{"rows": [';
    $categories .= '{"id": "1", "name": "Category 1"},';
    $categories .= '{"id": "2", "name": "Category 2"},';
    $categories .= '{"id": "3", "name": "Category 3"}';
    $categories .= "]}";
    return $categories;
}
echo GetCategories(); // sent to client
?>

注意:使用
JSON_encode构建JSON字符串始终是一种最佳实践

请参见。

返回的JSON字符串是什么样子?@existdissolve它看起来像{'id':'1','name':'Category 1'},{'id':'2','name':'Category 2'},{'id':'3','name':'Category 3'}它需要看起来像:{'rows:[{'id':'1','name':'Category 1'},{'id':'2','name':'Category 2'},{'id':'3','name':'Category 3'}}。您的代码中缺少一个连接。但是穆萨建议,如果您可以帮助的话,您确实不应该手工构建JSON。@existdissole I使用echo“{'id':'1','name':'Category 1'},{'id':'2','name':'Category 2'},{'id':'3','name':'categegory 3'}”;但它不起作用:(当你在开发者工具或Fire bug中查看请求时会返回什么?我发现define是假的?:(你确定它是针对ExtJS 4.1的吗?你发布的链接中的代码有一行,这意味着它是针对ExtJS 3的!它不适用于4.1。)