Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Basto

Problema com múltiplas requisições

Recommended Posts

Ae galera, to com um problema pra fazer multiplas requisições ajax. Uso um codigo parecido com esse:

var i = 0;function multipleRequests() {	var urls = new Array("data.xml", "ootest.htm", "url03.txt");	ajax = new ajaxObject(urls[i], "GET");	ajax.callBackFunction=function(ajaxResponse){		alert(ajaxResponse);	i++;	multipleRequests();	}	ajax.sendRequest();}

este código utiliza uma classe q eu to desenvolvendo ainda.. mas ela funciona perfeitamente com o método GET e pega resposta tanto em XML quanto Text. Aí vai o código dela:

/** * @author Bruno */// Constructorfunction ajaxObject(url, method) {	// Attributes	this.url			= url;	this.method		 = method;	this.requestObject  = null;	this.responseFormat = "Text";	// CallBack and error functions	this.callBackFunction = null;	this.errorFunction	= null;			// Methods	this.setResponseFormat   = setResponseFormat;	this.setCallBackFunction = setCallBackFunction;	this.setErrorFunction	= setErrorFunction;	this.getXmlHttpObject	= getXmlHttpObject;	this.sendRequest		 = sendRequest;}// Gives XMLHttpRequest object to our classfunction getXmlHttpObject(ajaxObject) {	if (window.XMLHttpRequest) {		ajaxObject.requestObject = new XMLHttpRequest();	} else if (window.ActiveXObject) {		ajaxObject.requestObject =  new ActiveXObject("Microsoft.XMLHTTP");	}	return ajaxObject;}// Sets the callBack functionfunction setCallBackFunction(callBackFunction) {	this.callBackFunction = callBackFunction;}// Sets the error functionfunction setErrorFunction () {}// Sets response formatfunction setResponseFormat(responseFormat){	this.responseFormat = responseFormat;	}// Sends the requestfunction sendRequest() {	ajaxObject =  getXmlHttpObject(this);	if (this.method == "GET") {		// Sending requests for the server with the GET method 		ajaxObject.requestObject.open(this.method, this.url, true);		ajaxObject.requestObject.send(null);	} else if (this.method == "POST") {		// Sending requests for the server with the POST method	}		ajaxObject.requestObject.onreadystatechange = function() {		if (ajaxObject.requestObject.readyState == 4) {			if (ajaxObject.requestObject.status == 200) {				if (ajaxObject.responseFormat == "XML") {					ajaxObject.callBackFunction(ajaxObject.requestObject.responseXML);					} else {					ajaxObject.callBackFunction(ajaxObject.requestObject.responseText);				}			} else {				ajaxObject.errorFunction();			}		} 	}}

ela é bem fácil de usar.. um exempo seria:

myAjax = new ajaxObject("myURL", "myMethod"); // Só pega com método GET por enquantomyAjax.setResponseFormat("Text"); // Text já é o default, não precisa declarar.myAjax.setCallBackFunction(myCallBackFunction); // Função a ser chamada qndo o URL for carregadomyAjax.setErrorFunction // Função a ser chamada caso não seja possível abrir o URL solicitadomyAjax.senRequest // Envia a requisição de fato.

Espero que poçam me ajudar.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.