Ver Mensaje Individual
  #3 (permalink)  
Antiguo 10/11/2009, 11:59
Avatar de dggluz
dggluz
 
Fecha de Ingreso: abril-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 525
Antigüedad: 15 años
Puntos: 50
Respuesta: como hacer peticiones con xmlhttprequest

Cita:
Iniciado por maycolalvarez Ver Mensaje
primero que todo el xmlhttprequest o AJAX por así decirlo se ejecuta en el cliente con Javascript y de allí envia una solicitud HTTP GET o POST hacia el server, esta solicitud llega a jsp y en la salida puedes devolver 2 cosas: o un texto plano o un xml, para poder procesarlo:

en javacript será más o menos así:

Código:
function Ajax() {
	var xmlhttpobj;
	function getajax(){
		try {
			xmlhttpobj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttpobj= new ActiveXObject("Microsoft.XMLHTTP");
			} catch (ex2) {
				xmlhttpobj= false;
			}
		}
		if (!xmlhttpobj && typeof XMLHttpRequest!='undefined') {
			xmlhttpobj = new XMLHttpRequest();
		}
		return xmlhttpobj;
	}
	
	this.cancelar= function(){
		xmlhttpobj.abort();
		var ajaxobject = this;
		ajaxobject.error("Cancelado por el usuario");
	}
	
	this.sendget = function(url,vars,rxml){
		var xmlhttpobj=getajax();
		var ajaxobject = this;
		xmlhttpobj.open ("GET", url+"?"+vars,true);
		xmlhttpobj.onreadystatechange=function(){
			if (xmlhttpobj.readyState==4){
				if (xmlhttpobj.status==200){
					if (rxml==true){
					//respuesta como XML
						ajaxobject.loadxml(xmlhttpobj.responseXML);
						
					}else {
						//respuesta plana:						ajaxobject.load(xmlhttpobj.responseText);					
					}
				}
			}else if (xmlhttpobj.readyState==2){
				ajaxobject.preload();
			}
			else if (xmlhttpobj.readyState==3){
				ajaxobject.interactive();
			}
			else if (xmlhttpobj.readyState==1){
				ajaxobject.loading();
			}
		}
		xmlhttpobj.send(null);
	}
	
//envia por POST
	this.sendpost = function(url,postvars,rxml){
		var xmlhttpobj=getajax();
		var ajaxobject = this;
		xmlhttpobj.open ("POST", url,true);
		xmlhttpobj.onreadystatechange=function(){
			if (xmlhttpobj.readyState==4){
				if (xmlhttpobj.status==200){
					if (rxml==true){
					
						ajaxobject.loadxml(xmlhttpobj.responseXML);
						
						//alert(xmlhttpobj.responseXML);
					}else {
						ajaxobject.load(xmlhttpobj.responseText);					
					}
				}
			}else if (xmlhttpobj.readyState==2){
				ajaxobject.preload();
			}
			else if (xmlhttpobj.readyState==3){
				ajaxobject.interactive();
			}
			else if (xmlhttpobj.readyState==1){
				ajaxobject.loading();
			}
		}

		xmlhttpobj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttpobj.send(postvars);
	}
	
	this.loading = function cargando(){};
	this.load = function cargado(text){};
	this.error = function error(motivo){};
	this.loadxml = function cargadoxml(xml){};
	this.preload = function precargado(){};
	this.interactive = function iterando(){};
	
}
y se usa:
Código:
function funcionjs(texto){
alert('salida desde el server '+texto);
}

function ejecutarajax(){
var a= new Ajax();
	a.load= funcionjs;
	a.sendget('pagina.jsp',"variable="+valor,false);
}
Buena respuesta, muy detallada. Sin embargo tengo que añadir que desde el servidor se puede devolver también un objeto JSON y en ocasiones esto es muy práctico. ¡Suerte!