My question is very similar to this one .
There is a file with information in .json, which is stored next to main.html. In main.html I wrote the editor of this json.
The idea is that I download my own file (sorry, I haven’t found it easier, I will be happy for help), change it in the editor, and then overwrite it again.
Downloading and processing:
function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/text"); xobj.open('GET', '../data.json', true); // Replace 'my_data' with the path to your file xobj.onreadystatechange = function () { if (xobj.readyState == 4 && xobj.status == "200") { // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode callback(xobj.responseText); } }; xobj.send(null); } document.getElementById('submit').addEventListener('click',function() { // Get the value from the editor var userJson = editor.getValue(); console.log("userJson:", userJson); loadJSON(function(response) { var previousJson = JSON.parse(response); console.log("previousJson:", previousJson); var newJson = previousJson; var days = ["Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота"]; var i = 0; for (var i = 0; i < 6; i++) { newJson[userJson["Класс"] + "_lessons"][i] = userJson[days[i]]; } console.log("newJson:", newJson) // тут должно быть сохранение }); }); The problem is that I do not know how to overwrite.
That is: there is a variable newJson , which must be output to the file " ../data.json ". What should I do?
JSON.stringify(data), on the server accept it andfs.writeFile- Artem Gorlachevphp? - Raz Galstyan