Search code examples
javascriptjsonxmlhttprequest

Do I need an XMLHttpRequest to change a JSON file value with JavaScript?


So I'm trying to make a game where people can change their display name and then store it in a JSON file so that other files and pages can access it. So I added an XMLHttpRequest but I then read this article and wondered whether I actually need the XMLHttpRequest. I will attach the JavaScript and JSON files to give a bit more context.

JavaScript:

var console;
let requestURL = 'displayName.json';
let request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
function sendRequest() {
request.send("displayName");
}
document.getElementById('displayName');
if (displayName = "") {
  displayName = 'Soldier';
}

JSON:

displayNameData {
  "displayName": ""
}

Solution

  • If you want the user's browser to send information to your server, you need an HTTP request.
    For this reason, they are very important to web development and worth your time to learn about.
    Check out one or both of these resources to dive in:

    Note that communication with the server generally happens asynchronously so the browser can keep working on other things until the server responds. Therefore, to notice when the server responds, you need to use either

    Happy coding!