我正在嘗試從本地 Json 獲取 API。我的 console.log 顯示了我真正想要的for loop. 但我的output.innerHTML不起作用。我首先嘗試使用我的<div></div>標簽,然后將其轉換為pre標簽。仍然不起作用。它只顯示一條線。我想念什么?
資料.json:
"resultCode": "0",
"resultDescription": "success",
"body": {
"intraDayTradeHistoryList": [
{
"id": 444121195,
"date": "2022-01-26T00:00:34.000 0300",
"conract": "PH22012603",
"price": 731.99,
"quantity": 5
},
{
"id": 444121022,
"date": "2022-01-26T00:00:35.000 0300",
"conract": "PH22012603",
"price": 732,
"quantity": 5
},
{
"id": 444121234,
"date": "2022-01-26T00:00:43.000 0300",
"conract": "PH22012603",
"price": 731.99,
"quantity": 5
},
{
"id": 444120877,
"date": "2022-01-26T00:00:56.000 0300",
"conract": "PH22012608",
"price": 1341.99,
"quantity": 10
},
{
"id": 444121289,
"date": "2022-01-26T00:00:56.000 0300",
"conract": "PH22012608",
"price": 1341.98,
"quantity": 10
},
{
"id": 444121519,
"date": "2022-01-26T00:00:57.000 0300",
"conract": "PH22012608",
"price": 1342,
"quantity": 120
},
{
"id": 444120887,
"date": "2022-01-26T00:00:58.000 0300",
"conract": "PH22012608",
"price": 1342.88,
"quantity": 50
},
{
"id": 444121529,
"date": "2022-01-26T00:00:59.000 0300",
"conract": "PH22012608",
"price": 1342.9,
"quantity": 65
},
{
"id": 444120892,
"date": "2022-01-26T00:01:00.000 0300",
"conract": "PH22012608",
"price": 1343.8,
"quantity": 100
},
]};
index.html(省略硬代碼):
<body>
<pre id="output"><br></pre>
<script src="main.js"></script>
</body>
main.js 檔案:
const output = document.getElementById("output");
const jsonData = fetch("./data.json")
.then(res => res.json())
.then(data => {
//const dataLen = data.body.intraDayTradeHistoryList.length;
const tradeHist = () =>
{
for(let i = 0; i <= 5;i ){
output.innerHTML = (JSON.stringify(data.body.intraDayTradeHistoryList[i]));
console.log(JSON.stringify(data.body.intraDayTradeHistoryList[i]));
}
}
tradeHist();
})
.catch(error => console.log("Error"))
uj5u.com熱心網友回復:
正如我的評論中所解釋的,這是固定代碼:
const jsonData = fetch("./data.json")
.then(res => res.json())
.then(({body: {intraDayTradeHistoryList}}) => {
let html = '';
for (const entry of intraDayTradeHistoryList){
html = JSON.stringify(entry);
}
output.innerHTML = html;
})
.catch(error => console.log(error))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/455159.html
標籤:javascript
下一篇:加載資源失敗:net::ERR_CONNECTION_REFUSEDhttp://127.0.0.1:3000/api/categories
