So I am trying to create a simple ajax request that returns hello world text from data.txt (in same root directory).
PROBLEM: When checking the status === 200 if statement doesn't display anything i.e. its not returning true.
TWIST: But if the if statement is removed the request is logged onto the console (but the data is not being written on the page.
CODE
// AJAX REQUEST EXAMPLE
// XHR is the api that is used for AJAX REQUESTS
// Create a XHR request object
var request = new XMLHttpRequest();
// create the 'request' for this object. open() reqest method GET/POST, location of the data file (ajax requests has same domain poilcy - so you can't request data objects for domains from other than what your currently on), true/fasle (whether we want request ot be asyncronous or not fasle means its asyc i.e. brwoser waits until requests is done before it does anything else )
request.open('GET', 'data.txt', true);
// send request to the server for data
request.send();
if (request.status=== 200) {
document.writeln(request.responseText);
}
console.log(request);