Retornar o responseText

1 resposta
R

Olá pessoal!

Mau problema é o seguinte: estou querendo retornar o responseText a partir de um método de um objeto javascript.
O objeto é esse:

/**
 ********************************************************
 * Objeto: Ajax
 * Descrição: Implementação de funcionalidades AJAX
 * Autor: Rômulo Augusto
 ********************************************************
 */

Ajax.prototype.responseText = null;
Ajax.prototype.xmlhttp = null;

/**
 * Ajax - construtor
 */
function Ajax()
{
	this.responseText = "";
	this.xmlhttp = this.getXMLHttpRequest();
}

/**
 *
 */
Ajax.prototype.getXMLHttpRequest = function()
{
	if(this.xmlhttp == null)
	{
		try
		{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		}
		catch(e)
		{
			try
			{
		    	this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		    }
		    catch (E)
		    {
		    	this.xmlhttp = false; 
		    }
		}
		
		if(!this.xmlhttp && typeof  XMLHttpRequest != 'undefined' )
		{
			try
			{
		    	this.xmlhttp = new XMLHttpRequest(); 
		    }
		    catch(e)
		    { 
		        this.xmlhttp = false;
		    }
		}
	}
	
	return this.xmlhttp;
}

/**
 * Ajax - sendRequest:
 * Envia uma solicitação para a url especificada.
 * 
 * Parâmetros:
 *  - url: endereço para a solicitação.
 */
Ajax.prototype.sendRequest = function (url)
{
	var xmlhttp = this.getXMLHttpRequest();

	xmlhttp.open("get", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if(xmlhttp.status == 200 || xmlhttp.status == 0)
				this.responseText = xmlhttp.responseText;
		}
	}
	
	xmlhttp.send(null);
}

/**
 *
 */
Ajax.prototype.getResponse = function()
{
	return this.responseText;
}

e o teste é esse:

<head>
    |script type="text/javascript"|
        function teste()
	{
		var ajax = new Ajax();
		ajax.sendRequest("texto.txt");

		alert(ajax.getResponse());
	}
    |/script|
</head>
<body>
    <input type="button" value="Enviar" onclick="javascript:teste();" />
</body>

No “alert” não vem resposta, vem “”.

Já procurei pra caramba… se alguém puder me ajudar…
Valeu!

1 Resposta

R

Consegui resolver!

Ainda não sei se é possível fazer como eu estava tentando fazer, mas acabei de ver como funciona com uma função de “callback”. Deu certo pro meu problema!

Valeu!

Criado 24 de novembro de 2006
Ultima resposta 24 de nov. de 2006
Respostas 1
Participantes 1