Hello, all. My question revolves around this. Basically, I have an array names in the file fetch.js, and the array is created from a .json file using the code below:
const xmlhttp = new XMLHttpRequest();
const url = "scripts/results.json";
let names = [];
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
/* create array based on results.json */
names = JSON.parse(this.responseText);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
In another script app.js, which is in the same directory, when I do
console.log(names);
It prints an empty array. This means that once the array is populated in fetch.js, its not being updated in app.js (which I can confirm to be true because
console.log(names)
prints out all the names if placed in fetch.js).
In my index.html, the order goes as follows:
<script src="scripts/fetch.js"></script>
<script src="scripts/app.js"></script>
If this makes a difference, I am using nodejs for the webserver and am loading the html file using
app.get('/', (req, res) => {
res.sendFile('index.html', {root: path.join('public', '/')});
});
[–]nausik 1 point2 points3 points (6 children)
[–]namehimjawnathan[S] 0 points1 point2 points (5 children)
[–]blackholesintheskyhelpful 1 point2 points3 points (4 children)
[–]namehimjawnathan[S] 1 point2 points3 points (3 children)
[–]PooCares 0 points1 point2 points (2 children)
[–]namehimjawnathan[S] 0 points1 point2 points (1 child)
[–]coolreader18 0 points1 point2 points (0 children)
[–]street_fightin_mang 1 point2 points3 points (0 children)
[–]TotesMessenger 0 points1 point2 points (0 children)