更新:为了避免问题完全归结于相同的来源策略的可能性,我尝试在本地服务,所有资产都来自使用发球的http://localhost:4000
。它并没有解决这个问题。因此,编辑小提琴可能无法工作,因为相同的起源策略,但您可以看到那里的代码。
我试图使用可动性加载外部JSON,跳过读/正常化步骤(它从现有的表生成JSON )。这应该是支持的,但对我不起作用。
这是我在JSFiddle上的尝试。从文档中加载JSON (对我来说似乎不太有用)工作得很好,就像在小提琴中看到的那样。但是,从URL中提取它根本不起作用。
这是我的JavaScript:
// getting JSON from the document works, but of what use is that?
$(document).ready( function() {
$('#local').dynatable({
dataset: {
records: JSON.parse($('#music').text())
}
});
// getting JSON from a remote source fails:
$('#remote').dynatable({
dataset: {
ajax: true,
ajaxOnLoad: true,
ajaxUrl: '//www.dynatable.com/dynatable-ajax.json',
records: []
}
});
});
...which引用两个表,一个表的id为" local“,另一个表的id为"remote",并引用了包含本地表数据的脚本:
<h3>Remote JSON: Failing</h3>
<table class="table table-striped" id="remote">
<thead>
<th>Some Attribute</th>
<th>Some Other Attribute</th>
</thead>
<tbody>
</tbody>
</table>
<hr>
<h3>Local JSON: Succeeding</h3>
<table class="table table-striped" id="local">
<thead>
<th style="color: black;">Band</th>
<th>Album</th>
</thead>
<tbody>
</tbody>
</table>
<script id="music">
[
{
"band": "The Police",
"album": "Ghost In The Machine"
},{
"band": "Supertramp",
"album": "Breakfast In America"
},{
"band": "Peter Tosh",
"album": "Mama Africa"
},{
"band": "The Police",
"album": "Regatta d'Blanc"
},{
"band": "The Police",
"album": "Zenyatta Mondatta"
},{
"band": "Supertramp",
"album": "Crime of the Century"
},{
"band": "Talking Heads",
"album": "Remain in Light"
},{
"band": "Talking Heads",
"album": "Speaking in Tongues"
}
]
</script>
发布于 2014-01-24 09:26:45
远程无法工作的原因是,当通过AJAX获取数据时,响应JSON必须包含一些元数据以及实际记录。
如果查看动态文档中的AJAX示例,可以单击“查看AJAX数据”以查看格式:
{
"records": [
{
"someAttribute": "I am record one",
"someOtherAttribute": "Fetched by AJAX"
},
{
"someAttribute": "I am record two",
"someOtherAttribute": "Cuz it's awesome"
},
{
"someAttribute": "I am record three",
"someOtherAttribute": "Yup, still AJAX"
}
],
"queryRecordCount": 3,
"totalRecordCount": 3
}
您可以看到实际的结果数组嵌套在响应JSON中的"records"
下,它还返回集合中总共有多少条记录,以及当前集中有多少条记录。
dynatable需要这些元数据的原因是为了在表的底部执行分页和记录计数文本。由于您的服务器正在执行实际的查询、排序和分页(例如通过数据库查询或其他服务器端处理),所以dynatable只看到最终结果。因此,“自然”杂志永远不会知道:
唯一可以从返回的结果中推断出的是(3),即在当前页面的过滤/查询集中有多少条记录。因此,它需要从服务器返回的JSON来告诉它(1)是totalRecordCount
,(2)是queryRecordCount
。
当然,如果不需要所有这些,只需关闭分页和记录计数文本,并告诉dynatable,结果将位于服务器返回的JSON的根目录:
$('#remote').dynatable({
features: {
paginate: false,
recordCount: false
},
dataset: {
ajax: true,
ajaxOnLoad: true,
ajaxUrl: '//www.dynatable.com/dynatable-ajax.json',
records: []
},
params: {
records: '_root'
}
});
https://stackoverflow.com/questions/21297192
复制相似问题