【问题标题】:sending parsed JSON file to client but format is not HTML将解析的 JSON 文件发送到客户端,但格式不是 HTML
【发布时间】:2020-11-10 02:28:47
【问题描述】:

调用 API,并解析 JSON。现在想在接收页面中以表格格式显示解析的 JSOn。它以纯文本形式出现。 API url 仅用于测试目的,取自 WIKI,出于安全目的不能发布实际 API。

requiremtn 是将 HTMl 文件以表格格式作为输出发送。

app.js 文件:

    const express = require("express");
    const bodyParser = require("body-parser");
    const https = require("https");
    var fs = require('fs');
    const app = express();
    app.use(bodyParser.urlencoded({ extended: true }));

    app.get("/", function(req, res) {
      res.sendFile(__dirname + "/index.html");
    });

    app.post("/", function(req, res) {

      const url = "https://en.wikipedia.org/w/api.php? 
   format=json&action=parse&page=Anthony%20Martial";

      https.get(url, function(response) {

     https.get(url, (resp) => {
  let data = '';

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.
  resp.on('end', () => {
    const jsonDATA = JSON.parse(data).parse;
    var jsonText = jsonDATA.text;
    var jsonTitle = jsonDATA.title;
    var jsonPageid = jsonDATA.pageid

    var str = JSON.stringify(jsonText);

    var str1 = str.replace(/\\n/g, '');
    const jsdom = require("jsdom");
    const {
      JSDOM
    } = jsdom;

    const virtualConsole = new jsdom.VirtualConsole();

    const dom = new JSDOM(str1, {
      virtualConsole,
      runScripts: "dangerously",
      resources: "usable"
    });
    var strWrite = dom.window.document.querySelector("table").textContent;
    res.setHeader("Content-Type", "text/html");
    res.write(strWrite);
    res.send();
      });
     });
       });
    });
    app.listen(3000, function(req, res) {
     console.log("server is running in port 3000");
    });

index.html 文件:

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
    <head>
      <meta charset="utf-8">
      <title>weatehr App API</title>
      <script src="app.js" charset="utf-8"></script>
      
    </head>

    <body>

      <form action="/" method="post">
        <label for="cityInput">City Name:</label>
        <input id="cityInput" type="text" name="cityName">
         <button type="submit"> Go </button>
        <h3>hello</h3>
      </form>
  
      <script src="app.js" charset="utf-8"></script>
    </body>

    </html>

我收到的输出: 明文格式:

Anthony Martial武术2017年效力曼联个人资料全名Anthony Jordan Martial[1]出生日期 (1995-12-05) 1995 年 12 月 5 日(24 岁)[2]出生地点马西, FranceHeight1.81Â m (5Â ft 11 in)[3]上场位置前锋俱乐部信息当前 球队曼联号码9青年生涯2001-2009CO Les Ulis2009-2012里昂高级 职业生涯年 TeamApps(Gls)2012–2013Lyon B11(5)2013Lyon3(0)2013Monaco B4(3)2013-2015摩纳哥49(11)2015-曼联143(51)国家队-2010-2011法国 U1617(9)2011-2012法国 U1713(9)2012-2013法国 U184(3)2013法国 U195(0)2013-2015法国 U2112(4)2015–France18(1) 荣誉代表法国男子足球欧足联欧洲 冠军亚军 2016 年法国 高级俱乐部出场和进球计入国内 仅限联赛,截至 2020 年 7 月 16 日(UTC)21:16 时正确无误——国家队上限和进球数正确 截至 2018 年 3 月 27 日

【问题讨论】:

    标签: html node.js json html-parsing jsdom


    【解决方案1】:

    问题已解决。

    选项1: 已从字符串中删除了解析 JSON 转义字符的使用 repalce 语句。 现在完全以表格格式显示它。

    选项2: 连接并标记然后在末尾关闭标记。 然后它以表格的形式出现。

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 2015-10-04
      • 2013-10-15
      • 1970-01-01
      • 2023-03-24
      • 2011-06-28
      • 2016-08-30
      • 2020-12-31
      相关资源
      最近更新 更多