Hér eru nokkur dæmi u XMLHttpRequest-kóða.
function ajax(url, vars, callbackFunction) {
var beidni = new XMLHttpRequest();
beidni.open("POST", url, true);
beidni.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
beidni.onreadystatechange = function() {
var buid = 4, alltILagi = 200;
if (beidni.readyState == buid && request.status == alltILagi) {
if (beidni.responseText) {
callbackFunction(beidni.responseText);
}
}
};
beidni.send(vars);
}
var beidni = new XMLHttpRequest();
beidni.open("GET", url, false);
beidni.send(null);
if(!beidni.getResponseHeader("Date")) {
var geymt = beidni;
beidni = new XMLHttpRequest();
var efBreyttSidan = geymt.getResponseHeader("Last-Modified");
efBreyttSidan = (efBreyttSidan) ?
efBreyttSidan : new Date(0); // January 1, 1970
beidni.open("GET", url, false);
beidni.setRequestHeader("If-Modified-Since", efBreyttSidan);
beidni.send("");
if(request.status == 304) {
beidni = geymt;
}
}