Skip to content

Instantly share code, notes, and snippets.

@cmarkle27
Created August 4, 2012 01:10
Show Gist options
  • Save cmarkle27/3253238 to your computer and use it in GitHub Desktop.
Save cmarkle27/3253238 to your computer and use it in GitHub Desktop.
Basic Ajax Request
function ajaxRequest(target, method, params, callback, graph) {
var xmlhttp=false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
window[callback](xmlhttp.responseText);
} else if (xmlhttp.status.toString().substring(0,1) == 3 || xmlhttp.status.toString().substring(0,1) == 4) {
message('Die Anfrage lieferte kein Resultat.', true);
}
};
xmlhttp.open(method, target, true);
if ((method == 'POST' || method == 'post') && params.length > 0) {
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
} else {
xmlhttp.send(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment