I have an Object into a .json
file and it includes just an Array.
Now I would like to get a word from the user using prompt
, and add it to this Array; you can see the codes below :
function addWord() {
let myWords = getWords();
let newWord = prompt("What word would you like to be added to your list?", "");
myWords.push(newWord);
let myWordsJson = JSON.stringify(myWords);
let xhr2 = new XMLHttpRequest();
xhr2.open("GET", "words.json?wordsArray=" + myWordsJson);
xhr2.send();
}
And here is 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 debugged my code and there is no problem with receiving the Array from the server and adding the new word, but then I can not send the new Array to the words.json
file.
here is words.json
:
{"wordsArray" : ["hello", "pencil", "school", "tooth", "family", "class"]}