Last Updated: September 02, 2019
·
1.035K
· 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);
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside