【问题标题】:How to update json from within html?如何从 html 中更新 json?
【发布时间】:2018-08-01 19:07:57
【问题描述】:

首先,我要声明一个显而易见的事实:我不擅长 html。 我的 javascript 编码是相当的,但我现在才发现 js 在 html 中的严重限制。

案例:我的 node.js 应用程序将错误日志输出到 json。 我制作了一个动态 html 表格来排序和显示这些错误,所有这些都运行良好。

但是,当我处理完这些错误后,我希望在所述 html 页面上有一个按钮,用 [] 覆盖 json 文件,从而有效地将其再次变为空白。

现在,在 node.js 中,使用 fs 很容易,但在 html 中没有这样的东西。 为了读取 json,我求助于 XMLhttprequest。

我很确定答案是我无法从客户端更改服务器上的数据。 这是有道理的,但有解决方法吗?

<script>
    Parser();
    setInterval(function(){Parser()}, 300 * 1000)

    function Parser() {
        getJSON("Errors.json", function(err, data) {
            let array           = data;
            let titles          = ["Time", "Program", "", "Count", "Info", "Error"];
            let text            = [];
            let joined          = [];

            let table           = document.createElement("table");
            let tr              = table.insertRow(-1);
            for (let i in titles) {
                let th          = document.createElement("th");
                    th.innerHTML = titles[i];
                    tr.appendChild(th);
            }

            for (let i in array) {
                let count = 0;
                array[i].count = 1;
                for (let x in joined) {
                    let ar = array[i], jn = joined[x];
                    if (ar.file == jn.file && ar.line == jn.line && ar.info == jn.info && ar.message == jn.message){
                        count++;
                        jn.count++;
                    }
                }
                if (count == 0) joined.push(array[i])
            }
            for (let i in joined) {
                let tx          = joined[i]
                    text[0]     = tx.time;
                    text[1]     = tx.file;
                    text[2]     = tx.line;
                    text[3]     = "("+tx.count+"x)";
                    text[4]     = tx.info;
                    text[5]     = tx.message;

                tr = table.insertRow(-1);
                for (let j in text) {
                    let tabCell = tr.insertCell(-1);
                        tabCell.innerHTML = text[j];
                }
            }

            let Table = document.getElementById("table");
                Table.innerHTML = "";
                Table.appendChild(table);
        });
    };

    function getJSON(url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.responseType = "json";
        xhr.onload = function() {
          let status = xhr.status;
          if (status === 200) {
            callback(null, xhr.response);
          } else {
            callback(status, xhr.response);
          }
        };
        xhr.send();
    };
</script>

【问题讨论】:

  • HTML 纯粹是描述性的。它实际上什么都不做,浏览器获取 HTML 并基于它呈现文档。浏览器完成所有工作,这就是 javascript 的用武之地。

标签: javascript html json fs


【解决方案1】:

您发布的代码示例与您似乎自己回答的实际问题无关。您无权从浏览器访问文件系统。

但是,您可以让按钮向后端服务器发送 AJAX 请求,以便为您清除 JSON 文件。

【讨论】:

  • 有什么“傻瓜”教程可以让我入门吗?我的后端只是一个在服务器上运行的 nodjes 应用程序,它将错误吐出到一个 json 文件中。没有 ajax 或你有什么参与...
  • 您的getJSON() 发送一个ajax 请求。所以你可以有这样的东西getJSON('/clear/log/file', ...)。只需确保在您的后端处理 /clear/log/file 的路由以执行您想要执行的操作。
猜你喜欢
  • 2011-05-03
  • 1970-01-01
  • 2011-01-06
  • 2023-03-22
  • 2019-11-24
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多