简体   繁体   中英

Edit and save the json file data on the disk using Jquery Javascript

I want to edit and write the data into the file which is on the disk and I want to save changes back to the same file using jquery. For this i have a button and textarea where I have edited the data and get the data before editing from json file as html5 :

 function saveFile(data){ $.getJSON("../custom_scripts/sub.json", function(data){ var updatedData = $("#result").val(); $("sub.json").replaceWith(updatedData); }); } 
  <form class="fileForm"> <input type="file" value="Choose file" onchange="onFileSelected(event)"> <br><br> <textarea id="result" rows="20" cols="100"></textarea> <button type="button" id="savable" class="btn btn-default" title="Save Changes" onclick="saveFile();"> Save</button> </form> 

After selecting the file the data gets edited and the additional data which I have entered is the updatedData but it

you can't write a JSON to a file on the clientside due to security reasons. (Otherwise you have access to the filesystems of your website's users.) You would have to use a server-side language for this, and store the file on the server-side.

For security, you can't write on client's disk.
The best way to save localy is to use localStorage .
You shoul read that page from MDN about webStorage.
some samples here.

var myContent = 'I am the content to save';
localStorage.setItem('mySaved', myContent);

and later to retrieve it :

 var myContent = localStorage.getItem('mySaved');

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