I'm facing an issue where the JSON data I'm trying to fetch from a URL is not displaying due to an uncaught reference error in my code. How can I modify the code to ensure that the data gets shown?
var url = "https://fantasy.premierleague.com/api/entry/1258872/";
req.open("GET", url);
req.send();
req.addEventListener("load", function(){
if(req.status == 200 && req.readyState == 4){
var response = JSON.parse(req.responseText);
document.getElementById("id").textContent = response.title;
document.getElementById("player first name").textContent = response.player_first_name;
document.getElementById("player last name").textContent = response.player_last_name;
}
})
<h1>Fantasy Premier League</h1>
<h2 id="id"></h2>
<h3>First Name: <span id="player first name"></span></h3>
<h3>Last Name: <span id="player last name"></span></h3>