quinta-feira, 6 de agosto de 2009

Ajax com Callback

Esta a é uma função que fiz (com base em outra que encontrei na internet) para fazer requisições ajax e enviar o texto da resposta para uma função de Callback:


function LoadAjax(Page, CallBack, Metodo){
if (Metodo == null){
Metodo = "GET";
}
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Erro ao carregar o programa!");
return false;
}
}
}
xmlHttp.open(Metodo,Page,true);
xmlHttp.send(null);

xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
Resposta = xmlHttp.responseText;
CallBack(Resposta);
}
//opcional
else{

}
}
return false;
}

Nenhum comentário:

Postar um comentário