Last Updated: September 02, 2019
·
815
· dannygarcia

Simple AJAX Function

// Simple ajax function to avoid using jQuery (°-°)
var ajax = function (url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.send();
    xhr.onreadystatechange = function (e) {
        if (e.target.status === 200 && e.target.readyState === 4) {
            callback(e.target.response);
        }
    };
};

// Example
ajax("http://news.ycombinator.com/", function (data) {
    console.log(data);
});