I have stored an Object in a .json file that contains only an Array.
Now I want to prompt the user for a word and add it to this Array. Below are the relevant code snippets:
function addWord() {
let myWords = getWords();
let newWord = prompt("Please enter a word to add to your list:", "");
myWords.push(newWord);
let myWordsJson = JSON.stringify(myWords);
let xhr2 = new XMLHttpRequest();
xhr2.open("GET", "words.json?wordsArray=" + myWordsJson);
xhr2.send();
}
Here is the getWords() function:
function getWords() {
let xhr = new XMLHttpRequest();
xhr.open("GET", "words.json", false);
xhr.send();
let myCode = JSON.parse(xhr.responseText);
return myCode["wordsArray"];
}
I have tested my code and successfully retrieved the Array from the server, added the new word, but encountered issues when trying to save the updated Array to the words.json file.
Below is the content of words.json:
{"wordsArray" : ["hello", "pencil", "school", "tooth", "family", "class"]}