我正在尝试使用以下javascript将公共json文件用作数据源:
const url =“ https://data.melbourne.vic.gov.au/resource/ez6b-syvw.json”;
var table = new Tabulator(“#example-table”,{ ajaxConfig:{ 方法:“获取” }, ajaxURL:url,// ajax URL 索引:“ MessageID”, autoResize:true, layout:“ fitData”,//布局选项 占位符:“正在等待数据...”, 列:[//定义表列 {title:“ timestamp”,字段:“ timestamp”}, {title:“温度”,字段:“ temp_avg”}, {title:“湿度”,字段:“ humidity_avg”}, {title:“ latitude”,field:“ latitude”}, {title:“经度”,字段:“经度”},
],
ajaxResponse:function(url, params, response){
//url - the URL of the request
//params - the parameters passed with the request
//response - the JSON object returned in the body of the response.
return response; //pass the data array into Tabulator
},
rowClick:function(e, row){ //trigger an alert message when the row is clicked
alert("Row " + row.getData().MessageId + " Clicked!!!!");
},
});
但是我在Chrome中收到以下控制台消息:
从原点'https://data.melbourne.vic.gov.au/resource/ez6b-syvw.json'到'http://192.168.0.6'的提取访问已被CORS策略阻止:飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段access-control-allow-origin。 data.melbourne.vic.gov.au/resource/ez6b-syvw.json:1无法加载资源:net :: ERR_FAILED tabulator.min.js:5 Ajax加载错误-连接错误:TypeError:无法获取 (匿名)@ tabulator.min.js:5 tabulator.min.js:5 Ajax加载错误:TypeError:无法获取
我不知道我在做什么错?
请帮助。
谢谢。
看来从资源中获取数据时出现问题。制表符似乎总是发送Access-Control-Allow-Origin
,但是您的资源不允许这样做。因此,一种解决方法是使用您自己的fetch
请求而不是Tabulator
ajax选项。然后,您可以在表实例上使用setData
。
这里是一个例子。 https://jsfiddle.net/nrayburn/8wd3zo9q/33/
我不确定这是否是错误,或者我只是没有找到解决方法。