简单的Kendo UI远程DataSource返回空

时间:2013-03-05 05:04:31

标签: javascript json kendo-ui

这是一个简单的例子,我正在尝试构建一个练习,我的DataSource对象返回时没有数据。

var data = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "data.json",
                    dataType: "json"
                }
            }
        });

        console.dir( data );

data.json

[
    {
        "text": "Brand One"
    },
    {
        "text": "Brand Two"
    },
    {
        "text": "Brand Three"
    },
    {
        "text": "Brand Four"
    }
]

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您的代码存在两个问题

  1. 首先,您需要调用 data.read() - 以便执行请求
  2. 由于上述操作是异步操作,如果在使用 data.read()后立即调用,则调用 data.data()将不会返回任何内容。要等到检索数据,您需要使用requestEnd事件。

答案 1 :(得分:-1)

我可以将load数组加载到datasource并返回object,参见

var data_input= [
    {
        "text": "Brand One"
    },
    {
        "text": "Brand Two"
    },
    {
        "text": "Brand Three"
    },
    {
        "text": "Brand Four"
    }
];

var data = new kendo.data.DataSource({
            transport: {
                read: {
                    data: data_input,
                    dataType: "json"
                }
            }
        });

console.log(data)