I'm making a website for my battlefield 1 clan, on this website i would like to display every player in this clan and also some of their in-game stats. The player list is stored in a database and i will get the stats from this api . This means that i will first get the player list from the database using ajax and then loop through them to get the player stats through a second ajax call in that loop.
It all sounds fun and games till i run my code, sometimes not all of the requests succeed and whenever i'm trying to display a displayname it will always show the one from the last call.
This is my code:
$(document).ready(function() {
$.ajax({
url: 'playerlist.php',
method: 'POST',
data: {
playercheck: 1,
},
success: function(response) {
var len = response.length;
for (var i = 0; i < len; i++) {
var psnid = response[i].psnid;
// second ajax
var request = new XMLHttpRequest();
request.open('GET', 'https://battlefieldtracker.com/bf1/api/Stats/BasicStats?platform=2&displayName=' + psnid);
request.setRequestHeader('TRN-Api-Key', '125a7cbe-1bbe-45d4-9f70-3aa838fc7535');
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status == 200) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
var result = JSON.parse(request.responseText);
console.log(result);
$("#userTable").append(result['profile']['displayName']);
}
};
request.send();
// end second
}
},
dataType: "json"
});
});
If you guys could tell me what is causing this and help me find a solution, that would be great. Thanks in advance!