【问题标题】:Kendo Datasource response is emptyKendo 数据源响应为空
【发布时间】:2013-09-20 15:25:07
【问题描述】:

我是 Kendo UI 的新手。我正在尝试读取远程数据并显示在屏幕上。我可以通过访问浏览器中的 URL 来查看远程 json 数据。但是当我尝试 alert() kendo UI 中的响应数据时,它是空的。

这里是示例代码。

<script type="text/javascript">
    var shareDataSource;
    var viewModel;
    function searchByVin() {
        var vin = $("#vinNumber").val();
        shareDataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "http://localhost:9080/portal/programexcep/resources/request/vin/"+vin,
                    dataType: "json"
                }
            },
            schema: {
                data: "data",
                model: { id: "Id" }
            },
            requestEnd: function(e) {
                var response = e.response;
                var type = e.type;
                alert(type);
                alert(response.length);
            }
        });
        shareDataSource.read();
        alert(vin);
        alert(kendo.stringify(shareDataSource.data()));
    }
</script>

JSON 数据是

{"Id":10,"FirstName":"John Smith","vin":"html5"} 

作为浏览器中的响应。 alert(kendo.stringify(shareDataSource.data())); 为空 alert(response.length); 也是未定义的。

有人可以帮忙吗?

【问题讨论】:

    标签: kendo-ui kendo-datasource


    【解决方案1】:

    问题在于shareDataSource.read(); 是异步的,这意味着您调用读取但数据不是立即可用的。您可以使用fetch,而不是在数据可用时执行部分代码。你的代码是:

    shareDataSource.fetch(function() {
        alert(vin);     
        alert(kendo.stringify(shareDataSource.data()));
    });
    

    requestEnd 函数中还有一个问题:您尝试获取responselength,但在model 定义中您说data 字段被称为data,因此您的服务器应该是返回类似:

    { 
        "data" : [ 
            { "Id" : 1 },
            { "Id" : 2 }, 
            { "Id" : 3 }
        ]
    }
    

    然后为了访问你应该做的长度response.data.length

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多