简体   繁体   English

无法在客户端编辑json文件

[英]unable to edit the json file at client side

i want to edit an json file using javascript. 我想使用JavaScript编辑json文件。 the file is "client.json" and is at the local system and is modified at local system only. 该文件为“ client.json”,位于本地系统,仅在本地系统上被修改。 I am using form for creating the new client details and i want that those details to be appended to the json file as the object. 我正在使用用于创建新客户端详细信息的表单,我希望将这些详细信息作为对象附加到json文件中。

 {"clients":[
        {
            "name":"xxxx",
            "team":"yyy",
            "location": {
                "city":"beijing",
                "country":"china"
                        },
        "wdays":{"start":"Monday","end":"friday"},
        "whours":{"start":"9 A.M","end":"6 P.M" }
        },
        {
            "name":"xxxx",
            "team":"yyy",
            "location": {
                "city":"new york",
                "country":"usa"
                       },
        "wdays":{"start":"Monday","end":"friday"},
        "whours":{"start":"9 A.M","end":"6 P.M" }
        }
]}

the following the javascript code is about reading data from form and appending that data as the json object.........` 以下javascript代码是关于从表单读取数据并将该数据附加为json对象.........`

</script>
<script type="text/javascript">
    function addtojson()
    {
       var client_name=(document.getElementById("cname")).value;
       var country=document.getElementById("country").value;
       var city=document.getElementById("city").value;
       var wtimeto=document.getElementById("wtimeto").value;
       var wtimefrom=document.getElementById("wtimefrom").value;
       var wdayto=document.getElementById("wdayto").value;
       var wdayfrom=document.getElementById("wdayfrom").value;
    jQuery.getJSON('client.json')
  .done(function(data) {
  var cobj=new Object();
  cobj.name=client_name;
  cobj.team="YYY";
  cobj.location=new Object();
  cobj.location.city=city;
  cobj.location.country=country;
  cobj.wdays=new Object();
  cobj.wdays.start=wdayto;
  cobj.wdays.end=wdayfrom;
  cobj.whours=new Object();
  cobj.whours.start=wtimeto;
  cobj.whours.end=wtimefrom;
  var myString = JSON.stringify(cobj);
  alert('I am working');
  alert(myString);
   data.clients.push(cobj);
      alert(JSON.stringify(data.clients[0]));//it gives previous first array object
      alert(JSON.stringify(data.clients[1]));//it gives previous second array object
      alert(JSON.stringify(data.clients[2]));//it gives the new appended array object
      });


return true;
} 
</script>`

though the data is being modified locally in the program the json file is not getting updated???????? 虽然数据正在程序中本地修改,但json文件没有更新?

JavaScript in a browser is generally not permitted to write to the local disk. 通常不允许浏览器中的JavaScript写入本地磁盘。 Regardless, after you load the JSON file, you're editing the loaded document in memory, which has no impact on the file on the disk until you write the changes to disk. 无论如何,在加载JSON文件之后,您都将在内存中编辑加载的文档,这对磁盘上的文件没有任何影响,除非您将更改写入磁盘。 This is where you will run into the problem of not being allowed to write to disk from a script. 在这里您将遇到不允许从脚本写入磁盘的问题。

You might be able to get something going using dynamically-generated data: URI's to trigger a download prompt to download the new version of the file and save it to disk. 使用动态生成的数据,您也许可以使事情变得顺利:URI触发下载提示,以下载文件的新版本并将其保存到磁盘。 See http://en.wikipedia.org/wiki/Data_URI_scheme for more info. 有关更多信息,请参见http://en.wikipedia.org/wiki/Data_URI_scheme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM