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);
});Written by Danny Garcia
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Function
Authors
Chip Castle
22.39K
deleteman
10.37K
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#