gpt4 book ai didi

javascript - JSON获取并在html页面上显示

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:47 25 4
gpt4 key购买 nike

我正在制作一个网络应用程序,我需要从 JSON 文件获取数据并以表格的形式打印它们。问题是我收到 404 错误,JSON 文件(实际上我在本地工作并执行 Node.js)。

编辑 1:
在查看网络选项卡时,我看到 404 错误。 JSON 文件位于text/file.json 中,index.ejs 位于views/index.ejs 中。所以我的问题是,在尝试获取 JSON 文件时无法获取任何数据(找不到)。

该应用程序位于使用 Node.js 和 Express 制作的本地服务器上。

我的文件树

  • text/allfiles.json
  • 公开/
  • 路线/index.js
  • views/index.ejs
  • app.js

这是我的文件:index.ejs

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(function() {
$.getJSON("/text/global_summary.json", function(data) {
console.log("JSON Data opened");
$.each(data, function(i, field) {
var tblRow = "<tr>" + "<td>" + field.topic + "</td>" +
"<td>" + field.page + "</td>"
$(tblRow).appendTo("#userdata tbody");
});

});
});
</script>
.
.
.
<div class="container">
<table id= "userdata" border="2">
<thead>
<tr>
<th data-field="id">Topic</th>
<th data-field="name">Page</th>
</tr>
</thead>

<tbody>
</tbody>
</table>
</div>

我的 json 文件

  [{"topic": "INTRODUCTION", "page": 8}, {"topic": "THE UNIVERSE", "page": 10}]

谢谢。

最佳答案

我会检查您是否可以通过将 json 文件的 url 直接放入浏览器的 url 窗口来查看/下载该文件。

如果这不起作用(404 强烈建议这样做),则您的 Express 应用程序不会提供 json 文件,您必须添加提供静态文件或为 json 文件添加路由。

另外:您的应用程序是否托管在网络服务器的根目录下?因为以/开头的 url 会寻址网络服务器根目录下的文件。

关于javascript - JSON获取并在html页面上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com