Created
September 12, 2013 19:11
-
-
Save MontealegreLuis/6542428 to your computer and use it in GitHub Desktop.
Ajax function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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