Simple XMLHttpRequest

I’ve never written my own XMLHttpRequest; I’ve always used jQuery or D3’s libraries. I had some sneaking suspicion one could do better than those wrappers in a modern HTML 5 setting, particularly for error handling, so for this project I wrote my own XHR code.

And turns out, nope, I’m not doing anything clever that those other libraries doesn’t. I’m doing less, really, my error handling is just as bad and there’s some clumsy stuff. Also this code only handles a really well behaved service; hasn’t been battle hardened against perverse replies or old browser versions. It does work for me though.

// Make AJAX calls, assuming JSON replies
function jsonRequest(url, callback) {
    var req = new XMLHttpRequest;"GET", url, true);
    req.onreadystatechange = function() {
        if (req.readyState === 4) {
            if ((req.status >= 200 && req.status < 300) || req.status == 304) {
                var d = JSON.parse(req.responseText);
            } else {
                // Bogus error handling