【问题标题】:Read Json file from local Directory when Offline and overwrite the Data when Online离线时从本地目录读取 Json 文件,在线时覆盖数据
【发布时间】:2018-03-26 20:08:41
【问题描述】:

我正在使用 Json Data 用详细信息列表填充我的 Html 表。我在 index.html 页面所在的同一目录中有名为 example.json 的 json 文件。但是当我在下面的代码中添加“example.json”时它不会显示/读取 json 文件,但是当我加载在线 url 时工作得很好。 我想用本地存储中的 Json 文件填充表,并在在线时自动用新数据覆盖 example.json 的先前数据。

这是我的 HTML

<!DOCTYPE html>
<html>
<head>

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	
</head>
<body>
<div class="container">
	
<div class="table-responsive">
	<h1>Json</h1>
	<br/>

	<table class="table table-bordered table-striped" id="employee_table">
		<tr>
			<th>Class</th>
			<th>Time</th>
			
		</tr>
	</table>
</div>
</div>

</body>

</html>



<script >
	

$(document).ready(function(){
	$.getJSON("https://api.myjson.com/bins/8qktd", function(data){
	var employee_data= '';
	$.each(data, function(key, value){
		employee_data += '<tr>';
		employee_data += '<td>'+value.class+'</td>';
		employee_data += '<td>'+value.time+'</td>';
		
		employee_data += '</tr>';
	});
	$('#employee_table').append(employee_data);

      });

});
</script>

一个

[

    {
    "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"
}

]

非常感谢任何帮助。我已经在寻找解决方案,但无处可寻。

【问题讨论】:

  • 使用本地网络服务器(如 xampp)来托管您的网站。这是一种很好的做法,它也可以解决这些类型的问题。
  • 你在哪个浏览器上尝试代码?
  • 我在 google chrome 上使用它,稍后我打算在 android Webview 中使用它。
  • @guest271314,我真的很感谢你的努力,但现在我正试图让它在桌面浏览器中工作,然后只有我会进入 webview。现在,我遇到了问题,不知道如何离线加载 json 文件(在线工作很好)但是当我切断互联网时,它没有显示任何数据。

标签: javascript jquery json html


【解决方案1】:

您可以在 HTML 中嵌入 JSON,例如在 data-* 属性中。如果navigator.onLine 从服务器获取JSON 修改HTML 并反映更新的JSON,否则使用嵌入在HTML 中的JSON。在此过程中的任何时候,您都可以使用与现有 HTML document 相同的文件名保存 HTML,以更新 HTML 中嵌入的 data-*

<!doctype html>
<html data-json='[{"id":"1","time":"10-15","class":"John Smith"}]'>

<head>
  <script>
    onload = () => {
      if (!navigator.onLine) {
        let json = document.documentElement.dataset.json;
        let data = JSON.parse(json);
        // do stuff with `data`
        console.log(data);
      } else {
        fetch("https://api.myjson.com/bins/8qktd")
          .then(response => response.json())
          .then(res => {
            console.log(res);
            // update HTML at at server to set `data-json` at `<html>`
            /*
            fetch("/path/to/server", {method:"POST", body:JSON.stringify(res)})
            .then(response => response.ok)
            .then(res => {
              console.log(res);
              document.documentElement.dataset.json = JSON.stringify(res);
              // download updated HTML
              const a = document.createElement("a");
              a.download = "file.html";
              a.href = URL.createObjectURL(new Blob([doucment.documentElement.outerHTML]));
              document.body.appendChild(a);
              a.click(); // download updated HTML with same file name
            })
            .catch(err => console.error(err));
            */
          });

      }
    }
  </script>
</head>

</html>

【讨论】:

  • 我应该如何将您提供的解决方案与我的脚本标签结合起来?当用户在线时,它是否会覆盖以前的数据,如果 json 文件填充了新信息并在之后加载相同的更新数据。当他们下线时
  • @BlueYeti24 如答案中所述,您将需要下载具有相同文件名的更新后的 HTML 文件,其中将包括在data-* 属性或其他嵌入方式的 HTML 中嵌入的更新后的JSON HTML 中的数据。 HTML 是无状态的;也就是说,您需要手动调整 HTML,无论是客户端还是服务器端,以反映所做的更改。
  • 好吧,我真的很感谢你的努力,但这不是我想要的,不可能更新整个 html ..我希望用最初嵌入在同一目录中的 json 填充 html 表,并且当用户上线并且如果数据被更改,则用新数据替换以前的 json 并在他/她在线或离线时继续加载新的更新数据,直到从服务器端更改 Json。
  • @BlueYeti24 如之前的评论所述,一个问题是 HTML 是无状态的。另外,file: 协议不同于http:https: 协议; localStorage 存储在完全相同的域中,您无法从file: 协议访问存储在https: 协议中的localStorage。另一种选择是使用ServiceWorker,参见Service Worker Sample: Custom Offline Page Sample
  • @BlueYeti24 不确定为什么不能保存整个 HTML?见Edit, save, self-modifying HTML document; format generated HTML, JavaScript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 2019-06-14
  • 2021-04-05
  • 1970-01-01
相关资源
最近更新 更多