Javascript - XMLHttpRequest - A simple GET example

By convention, we name the XMLHttpRequest variable xhr.

 
var xhr = new XMLHttpRequest();
xhr.open("GET", "/articles/javascript-xmlhttprequest-a-simple-get-example/book.json");
 
xhr.onreadystatechange = function () {
    console.log("readyState = " + this.readyState + ", status = " + this.status);
    if (this.readyState == 4 && this.status == 200) {
        var result = this.responseText;
        console.log(result);
    }
};
 
xhr.send();
 


Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath