脱机时从本地目录读取Json文件,并在联机时覆盖数据
我正在使用Json Data来填充我的Html表以及详细信息列表。 我在与index.html页面相同的目录中有一个名为example.json的json文件。 但是当我在下面的代码中添加“example.json”时,它不会显示/读取json文件,但是当我加载在线url时工作得很好。 我想用本地存储中的Json文件填充表,并且如果有自动的话,在线覆盖带有新数据的example.json的先前数据。
这是我的HTML
Json
Class Time
$(document).ready(function(){ $.getJSON("https://api.myjson.com/bins/8qktd", function(data){ var employee_data= ''; $.each(data, function(key, value){ employee_data += ''; employee_data += ''+value.class+' '; employee_data += ''+value.time+' '; employee_data += ' '; }); $('#employee_table').append(employee_data); }); });
一个
[ { "id":"1", "time":"10-15", "class":"John Smith" }, { "id":"2", "time":"10-15", "class":"John Smith" }, { "id":"3", "time":"10-15", "class":"John Smith" }, { "id":"4", "time":"10-15", "class":"John Smith" }, { "id":"5", "time":"10-15", "class":"John Smith" }, { "id":"6", "time":"10-15", "class":"John Smith" }, { "id":"7", "time":"10-15", "class":"John Smith" }, { "id":"8", "time":"10-15", "class":"John Smith" }, { "id":"9", "time":"10-15", "class":"John Smith" }, { "id":"10", "time":"10-15", "class":"John Smith" }, { "id":"11", "time":"10-15", "class":"John Smith" }, { "id":"12", "time":"10-15", "class":"John Smith" } ]
任何帮助将非常感谢。 我已经找到了解决方案,但找不到任何地方。
您可以在HTML中嵌入JSON
,例如在data-*
属性中。 如果navigator.onLine
从服务器获取JSON
修改HTML并反映更新的JSON
,则使用嵌入在HTML中的JSON
。 在此过程中的任何时候,您都可以使用与现有HTML document
相同的文件名保存HTML,以更新HTML中的嵌入data-*
。