[英]I can’t display the data from the json file on the html page
Script.js 和 test.json 位於“public”文件夾中。 我需要在 html 頁面中顯示來自 test.json 的數據。 腳本.js:
$(document).ready(function() {
let qqq = 'test.json';
let www = JSON.parse(qqq);
document.getElementById("eee").innerHTML = www;
});
服務器.js:
var express = require("express");
var path = require("path");
var app = express();
app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/index.html");
});
app.use(express.static(path.join(__dirname, "public")));
app.listen(8008);
console.log("Server has started!");
索引.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Report</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"
></script>
<script src="script.js"></script>
</head>
<body>
<div class="red" id="eee"></div>
</body>
</html>
測試.json:
{"tests": [
{ "testName": "testname1",
"value": "test_value",},
{ "testName": "testname2",
"value": "test_value",}
] }
let qqq = 'test.json'; //you are parsing test.json string.
您可以使用 ajax 從 json 文件中獲取數據,如下所示
$.ajax({
type: "Get",
url: "test.json",
dataType: "json",
success: function(data) {
let www = JSON.parse(data);
$("#eee").html(www);
},
error: function(){
alert("json not found");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.