using pure JavaScript/Ajax, I would like to take an array of file names(like: ["Data.txt", "UserInfo.txt", "Project.txt"]), and then load their data into an array.
The code would look like this:
var fileNames = ["Data.txt", "UserInfo.txt", "Project.txt"],
fileData = [],
client = new XMLHttpRequest();
client.onreadystatechange = function() {
if (client.readyState === 4) {
fileData.push(client.responseText);
};
};
for(i = 0; i < fileNames.length; i++){
client.open('GET', fileNames[i]);
client.send();
};
console.log(fileData);
. And that is what I tried to do, but it didn't work correctly.
How can I achieve this?
Thank you.
fileData.push = client.responseText? Doesn't look right. Also, if I'm not wrong, this looks like everyday ajax problem. When you logfileDatathe files are not returned by the response yet.