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!