3r6z2a
Last Updated: February 25, 2016
·
670
· 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);
});