Hello!
So I made a game in js and html.
Here is some code:
var jsonFile = “”
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(JSON.parse(xhttp.responseText)); //this works, and prints 138
var jsonFile = JSON.parse(xhttp.responseText)
You need to make use of the jsonFile inside the callback, like this:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(JSON.parse(xhttp.responseText)); //this works, and prints 138
var jsonFile = JSON.parse(xhttp.responseText)
document.getElementById(“playerScore”).innerHTML = "Coins: " + jsonFile
}
};
xhttp.open(“GET”, “/checkUserScore?username=” + username + “?password=” + password, true);
xhttp.send();
If you try to use it outside the callback, it hasn’t got a value yet. It’s only when the callback function is called that the value is set to something other than blank.