简体   繁体   中英

unable to edit the json file at client side

i want to edit an json file using javascript. the file is "client.json" and is at the local system and is modified at local system only. 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.

 {"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.........`

</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????????

JavaScript in a browser is generally not permitted to write to the local disk. 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. 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. See http://en.wikipedia.org/wiki/Data_URI_scheme for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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