Skip to content

Instantly share code, notes, and snippets.

@MontealegreLuis
Created September 12, 2013 19:11
Show Gist options
  • Save MontealegreLuis/6542428 to your computer and use it in GitHub Desktop.
Save MontealegreLuis/6542428 to your computer and use it in GitHub Desktop.
Ajax function
function CreateAjax() {
var objetoAjax = false;
try {
/* Para navegadores distintos a Internet Explorer */
objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
/* Para explorer */
objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
objetoAjax = false;
}
}
if (!objetoAjax && typeof XMLHttpRequest != 'undefined') {
objetoAjax = new XMLHttpRequest();
}
return objetoAjax;
}
function GetAjax(url, capa, metodo) {
var ajax = CreateAjax();
var capaContenedora = document.getElementById(capa);
/* Creamos y ejecutamos la instancia si el metodo elegido es GET */
if (metodo.toUpperCase() == 'GET') {
ajax.open('GET', url, true);
ajax.onreadystatechange = function() {
if (ajax.readyState == 1) {
capaContenedora.innerHTML = "Cargando...";
} else if (ajax.readyState == 4) {
if (ajax.status == 200) {
capaContenedora.innerHTML = ajax.responseText;
} else {
capaContenedora.innerHTML = "Error: ".ajax.status;
}
}
};
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax.send(null);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment