Hello there,
When I use AJAX to load a PHP file, the content of my function returns [object HTMLDivElement], but if I load my function without loading the PHP file, it displays normally.
index.php
<h1>API Football</h1>
<nav>
<ul>
<li>INDEX</li>
<li onclick="loadComponents()">Live fixtures</li>
</ul>
</nav>
<h2>Live fixtures</h2>
<div id="content">
</div>
<script src="javascript.js"></script>
javascript.js
var data = null;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
let liveFixtures = this.responseText;
let fixtures = liveFixtures.api.fixtures;
let content = "";
for (fixture of fixtures) {
content += "<tr>";
content += "<td>"+fixture.fixture_id+"</td>";
content += "<td>"+fixture.homeTeam.team_name+" - "+fixture.awayTeam.team_name+"</td>";
content += "</tr>";
}
}
});
xhr.open("GET", "https://api-football-v1.p.rapidapi.com/fixtures/live?timezone=Europe%2FLondon");
xhr.setRequestHeader("x-rapidapi-host", "api-football-v1.p.rapidapi.com");
xhr.setRequestHeader("x-rapidapi-key", "e8118d13f1msh0edd004389b2d85p1b89c2jsn0cd40324ddec");
xhr.send(data);
//Load content.php
function loadComponents() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("content").innerHTML = this.responseText;
document.getElementById("myDiv").innerHTML = content;
}
};
xhttp.open("GET", "content.php", true);
xhttp.send();
}
content.php
<div id="myDiv">
</div>
<p><strong>I loaded the content.php</strong></p>
Also available on .
Output from API Football
//let liveFixtures = {"api":{"results":18,"fixtures":[{"fixture_id":95187,"league_id":358,"league":{"name":"Serie B","country":"Brazil","logo":"https:\/\/media.api-football.com\/leagues\/358.PNG","flag":"https:\/\/media.api-football.com\/flags\/br.svg"},"event_date":"2019-11-08T22:15:00+00:00","event_timestamp":1573251300,"firstHalfStart":1573251300,"secondHalfStart":1573254900,"round":"Regular Season - 34","status":"Second Half","statusShort":"2H","elapsed":90,"venue":"Durival Britto e Silva, Curitiba","referee":null,"homeTeam":{"team_id":122,"team_name":"Parana","logo":"https:\/\/media.api-football.com\/teams\/122.png"},"awayTeam...