var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = getData;
httpRequest.open('GET', 'https://url/to/some.file', true);
httpRequest.send();
function getData(){
if (httpRequest.readyState === XMLHttpRequest.DONE) {
alert(httpRequest.responseText);
}
}
new XMLHttpRequest()
creates a new XMLHttpRequest object - this is what we will send our request with
The onreadystatechange
bit tells our request to call getData()
everytime it's status changes
.open()
creates our request - this takes a request method ('GET', 'POST', etc.), a url of the page you're querying, and optionally, whether or not the request should be asynchrynous
.send()
sends our request - this optionally accepts data to send to the server like .send(data)
finally, the getData()
is the function we've said should be called every time our request's status changes. if the readyState is equal to DONE then it alerts the responseText
which is just the data recieved from the server.
More info can be found in the getting started guide on MDN.