JSON文件使用fetch到HTML表
我想要一个 HTML 数据从 JSON 文件中获取数据。我正在使用 fetch,但我只是得到一张空白表。我查看了控制台,没有看到任何 CORS 错误。是什么阻止了它显示数据?
脚本
<script>
fetch ("https://gsx2json.com/api?id=1Jvqj6ArHXr0lqW7LR4P2Y0M4i0egcGX-3Ah4PFp4rvA&sheet=Sheet1").then((data)=>{
return data.json();
}).then((objectData)=>{
console.log(objectData[0].title);
let tableData="";
objectData.map((values=>{
tableData+='<tr>
<td>${values.Bill}</td>
<td>${values.Action}</td>
</tr>';
});
document.getElementById("table_body").
innerHTML=tableData;
})
</script>
表 (HTML)
<table class="table">
<thead>
<tr>
<th>Bill</th>
<th>Action</th>
</tr>
</thead>
<tbody id="table_body">
</tbody>
I would like to have an HTML data fetch data from a JSON file. I am using fetch, but I am just getting a blank table. I looked in Console and do not see any CORS errors. What is preventing it from displaying the data?
Script
<script>
fetch ("https://gsx2json.com/api?id=1Jvqj6ArHXr0lqW7LR4P2Y0M4i0egcGX-3Ah4PFp4rvA&sheet=Sheet1").then((data)=>{
return data.json();
}).then((objectData)=>{
console.log(objectData[0].title);
let tableData="";
objectData.map((values=>{
tableData+='<tr>
<td>${values.Bill}</td>
<td>${values.Action}</td>
</tr>';
});
document.getElementById("table_body").
innerHTML=tableData;
})
</script>
Table (HTML)
<table class="table">
<thead>
<tr>
<th>Bill</th>
<th>Action</th>
</tr>
</thead>
<tbody id="table_body">
</tbody>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我明白你想要做什么,但你的代码似乎有点混乱。在另一个文件中使用 javascript 可能是一个很好的建议。
我用你的方式向你展示了这是如何工作的。
有一些错误。
当数组是行或列时,您试图将 objectData 作为数组访问。因此,您可以像我一样使用
objectData.rows[index]
或解构。Okay, I understood what you are trying to do but your code seems to be a little bit messy. It could be a good suggestion to work with your javascript in another file.
I used your way to show you how this could work.
There are some mistakes.
You were trying to access to objectData as an array when the array is row or columns. So you could use
objectData.rows[index]
or destructure as I did.