Ver Mensaje Individual
  #2 (permalink)  
Antiguo 10/11/2009, 11:52
Avatar de maycolalvarez
maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 9 meses
Puntos: 1532
Respuesta: como hacer peticiones con xmlhttprequest

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");
	}
	
//envia como GET
	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);
}

Última edición por maycolalvarez; 10/11/2009 a las 11:58