我已在此问题中附加了我的代码。
我正在使用YUI-3从数据源以网格格式显示数据
我无法从数据源获取数据。但是数据是通过URL传递的。
我的网格始终显示No data to display
。
<script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>
<script>
YUI().use("datatable", "datasource-get", "datasource-jsonschema", "datatable-datasource", function (Y) {
var url = "http://domainname.com/trans/sample_json.php?format=json",
query = "&return=true",
dataSource,
table;
dataSource = new Y.DataSource.Get({ source: url });
dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "query.results.result",
resultFields: [
"Title",
"Phone",
{
key: "Rating",
locator: "Rating.AverageRating",
parser: function (val) {
// YQL is returning "NaN" for unrated restaurants
return isNaN(val) ? -1 : +val;
}
}
]
}
});
table = new Y.DataTable({
columns: [
"Title",
"Phone",
{
key: "Rating",
formatter: function (o) {
if (o.value === -1) {
o.value = '(none)';
}
}
}
],
width:"100%",sortable: true,
summary: "Pizza places near 98089",
caption: "Table with JSON data from Datasource...."
});
table.plug(Y.Plugin.DataTableDataSource, { datasource: dataSource });
table.render("#pizza");
table.datasource.load({
callback: {
success: function (e) {
table.datasource.onDataReturnInitializeTable(e);
},
failure: function() {
Y.one('#pizza').setHTML(
'The data could not be retrieved. Please <a href="?mock=true">try this example with mocked data</a> instead.');
}
}
});
});
</script>
这是sample_json.php
文件。我在这里以JSON格式发送数据。
<?php
echo '{"query": {"count": 2,"created": "2013-06-04T22:50:08Z","lang": "en-US","results": {"result":[{"Title" : "Giovannis Pizzeria","Phone" : "(408) 734-4221","Rating": {"AverageRating": "4"}},{"Title" : "Pizza","Phone" : "(800) 555-1212","Rating": {"AverageRating":"NaN"}}]}}}';
?>
我找不到错误在哪里。谁能帮我。?
your PHP doesn't look like it's returning JSON data
header('Content-Type: application/json');
echo json_encode($data);