无法在HTML页面上显示JSON文件中的数据。

huangapple go评论84阅读模式
英文:

I can’t display the data from the json file on the html page

问题

Script.js和test.json位于'public'文件夹中。我需要在HTML页面中显示test.json中的数据。
script.js:

$(document).ready(function() {

  let qqq = 'test.json';
  let www = JSON.parse(qqq);
  document.getElementById("eee").innerHTML = www;
});

server.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("服务器已启动!");

index.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>报告</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>

test.json:

{"tests": [
   { "testName": "testname1",
     "value": "test_value",},
   { "testName": "testname2",
     "value": "test_value",}
  ] }
英文:

Script.js and test.json are located in the 'public' folder. I need to display data from test.json in the html page.
script.js:

$(document).ready(function() {

  let qqq = &#39;test.json&#39;;
  let www = JSON.parse(qqq);
  document.getElementById(&quot;eee&quot;).innerHTML = www;
});

server.js:

var express = require(&quot;express&quot;);
var path = require(&quot;path&quot;);
var app = express();
app.get(&quot;/&quot;, function(req, res) {
  res.sendFile(__dirname + &quot;/views/index.html&quot;);
});

app.use(express.static(path.join(__dirname, &quot;public&quot;)));
app.listen(8008);
console.log(&quot;Server has started!&quot;);

index.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot; /&gt; &lt;title&gt;Report&lt;/title&gt;
    &lt;link  rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&quot;
&gt;&lt;/script&gt;
  &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;   
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;red&quot; id=&quot;eee&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

test.json:

{&quot;tests&quot;: [
   { &quot;testName&quot;: &quot;testname1&quot;,
     &quot;value&quot;: &quot;test_value&quot;,},
   { &quot;testName&quot;: &quot;testname2&quot;,
     &quot;value&quot;: &quot;test_value&quot;,}
  ] }

答案1

得分: 2

let qqq = 'test.json';  // 正在解析 test.json 字符串。

您可以使用以下方式通过 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 文件未找到");
    }
});
英文:
let qqq = &#39;test.json&#39;;  //you are parsing test.json string.

you can use ajax to get data from json file as following

$.ajax({
    type: &quot;Get&quot;,
    url: &quot;test.json&quot;,
    dataType: &quot;json&quot;,
    success: function(data) {
          let www = JSON.parse(data);
          $(&quot;#eee&quot;).html(www);
    },
    error: function(){
        alert(&quot;json not found&quot;);
    }
});

huangapple
  • 本文由 发表于 2020年1月3日 19:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定