Last active
March 2, 2022 10:50
-
-
Save kawanet/c4de2844ea69f17398d7061f8e1bab0a to your computer and use it in GitHub Desktop.
Minimal Ajax XHR GET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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