Simple ajax get

2012-05-28

Here's a very simple Ajax get. No attempt at edge casing, escaping, or cross browser fixes. Just xhr with a GET.

Code: (js)
var get = function(url, callback){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 || xhr.readyState=='complete') {
try { xhr.status; // we're just "touching" the value to trigger an exception if one has been thrown
} catch (errrr) {
// just do it for anything. this will also cause the error to show for 401 errors (unknown location), but it should never happen when not manipulating the script manually
throw("Warning: Unknown error with server request (timeout?).");
}
if (xhr.status == 200) callback(null, xhr.responseText);
else callback(new Error("File request problem (code: "+xhr.status+")!"));
}
};

xhr.open("GET", url);
xhr.send(null);
}

For are more extensive ajax script, see my old ajax script.