首页 > 解决方案 > 编辑本地 .json 文件

问题描述

我正在尝试通过我在它旁边创建的 .js 文件来编辑 .json 文件。我正在使用 ajax 调用从我的 .json 文件中获取数据,但对于编辑我不知道该怎么做,这是我获取数据的代码:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       objects =JSON.parse(xhttp.responseText) ;
      

    }
  };
  xhttp.open("GET", "object.json", true);
  xhttp.send();

这也是我的 .json 文件:

{
    "circles":[
        {  "id":1 ,
            "cx":100,
            "cy":100,
            "r":50,
            "stroke":"green" ,
            "strokeWidth":4,
            "fill":"yellow"
        },
        {
            "id":2 ,
            "cx":30,
            "cy":30,
            "r":10,
            "stroke":"green" ,
            "strokeWidth":4,
            "fill":"yellow"
        }
            ]
}

请给我一个线索谢谢你们:))

标签: javascriptajax

解决方案


您可以从前端从服务器获取数据,但要编辑数据,您需要创建一些后端功能。这可以是服务器或无服务器函数,也可以是托管的 db 调用,但您无法在没有后端的情况下编辑纯 .json 文件。


推荐阅读