Javascript 如何从html中更新json?

Javascript 如何从html中更新json?,javascript,html,json,fs,Javascript,Html,Json,Fs,首先,我将陈述一个显而易见的事实:我不擅长html。 我的JavaScript编码是标准的,但是我现在发现了HTML中JS的严重局限性。 案例:my node.js应用程序将错误日志输出到json。 我制作了一个动态html表来对这些错误进行排序和显示,所有这些工作都很好 但是,当我处理完这些错误后,我希望在html页面上有一个按钮,用[]覆盖json文件,使其再次变为空白 现在,在node.js中,使用fs很容易,但在html中没有这样的东西。 为了阅读json,我求助于XMLhttprequ

首先,我将陈述一个显而易见的事实:我不擅长html。 我的JavaScript编码是标准的,但是我现在发现了HTML中JS的严重局限性。 案例:my 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>

解析器();
setInterval(函数(){Parser()},300*1000)
函数解析器(){
getJSON(“Errors.json”,函数(err,data){
让数组=数据;
让标题=[“时间”、“程序”、“计数”、“信息”、“错误”];
让text=[];
让加入=[];
让table=document.createElement(“table”);
设tr=table.insertRow(-1);
对于(让我输入标题){
设th=document.createElement(“th”);
th.innerHTML=标题[i];
tr.appendChild(th);
}
for(让我进入数组){
让计数=0;
数组[i]。计数=1;
for(让x加入){
设ar=array[i],jn=join[x];
if(ar.file==jn.file&&ar.line==jn.line&&ar.info==jn.info&&ar.message==jn.message){
计数++;
j.count++;
}
}
if(count==0)加入.push(数组[i])
}
为了(让我加入){
设tx=joint[i]
text[0]=tx.time;
text[1]=tx.file;
text[2]=tx.line;
文本[3]=“(“+tx.count+”x)”;
text[4]=tx.info;
text[5]=tx.message;
tr=table.insertRow(-1);
for(让j在文本中){
设tabCell=tr.insertCell(-1);
tabCell.innerHTML=文本[j];
}
}
let Table=document.getElementById(“表”);
Table.innerHTML=“”;
表.appendChild(表);
});
};
函数getJSON(url,回调){
设xhr=newXMLHttpRequest();
xhr.open(“GET”,url,true);
xhr.responseType=“json”;
xhr.onload=函数(){
让status=xhr.status;
如果(状态===200){
回调(null,xhr.response);
}否则{
回调(状态,xhr.response);
}
};
xhr.send();
};

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


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

HTML纯粹是描述性的。它实际上什么也不做,浏览器接受HTML并基于它呈现文档。所有的工作都是由浏览器完成的,这就是javascript的用武之地。有没有“傻瓜版”教程可以让我开始学习呢?我的后端只是一个运行在服务器上的nodjes应用程序,它向json文件抛出错误。没有ajax或您参与的内容…您的
getJSON()
发送ajax请求。因此,您可以使用类似以下内容的
getJSON('/clear/log/file',…)
。只需确保在后端处理
/clear/log/file
的路由即可执行您想要执行的操作。