Skip to content

Instantly share code, notes, and snippets.

@kawanet
Last active March 2, 2022 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kawanet/c4de2844ea69f17398d7061f8e1bab0a to your computer and use it in GitHub Desktop.
Save kawanet/c4de2844ea69f17398d7061f8e1bab0a to your computer and use it in GitHub Desktop.
Minimal Ajax XHR GET
(function(url, cb) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
var err, res;
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 300) {
try {
res = JSON.parse(req.responseText);
} catch (e) {
err = e && e.message || e;
}
} else {
err = req.statusText || req.status;
}
}
cb(err, res);
}
req.open("GET", url);
req.send();
})("https://example.com/index.json", function(err, res) {
if (err) console.error(err);
// do something
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment