Witam. Jest to moja pierwsza przygoda z AJAXem i pierwszy problem.
Otorz po prostu tresc sie nie laduje. Tak wiem ze normalnie sie tak nie wczytuje tresci strony ale to tylko nauka.
JavaScript
function fillBio() {
//Step zero - CREATE new object to hold the stuff
let Data = {};
var bioContent = getID("bioContent");
//Step one - CREATE XHR object
let XHR = new XMLHttpRequest();
//Step two - OPEN the JSON file
XHR.open("GET", "../content.json", true); //true for async
//Step three - SEND out a call
XHR.send();
//Step four - Listen for READY STATE == 4
XHR.onreadystatechange = function() {
if ((this.readyState === 4) && (this.status === 200)) {
//PARSE data from string to JSON
data = JSON.parse(this.responseText);
//Step five wait for the data to finish loading before injecting.
XHR.addEventListener("load", function() {
bioContent.innerHTML = data.h1;
bioContent.innerHTML += data.p1;
bioContent.innerHTML += data.p2;
})
}
}
}
JSON
{
"bio": {
"h1": "<h1>Hi! My name is Alex</h1>",
"p1": "<p>I'm a website designer, and a web developer</p>",
"p2": "<p>My goal is to make your website as good, as fast, and as light as possible</p>"
},
"projects": {
"P": "These are some projects I've had the pleasure of working on"
}
}
Fragment HTMLa
<div class="container" id="bioContent"></div>