PDA

View Full Version : I wrote a js wrapper, don't know why it's giving me an error.


hayson1991
01-13-2008, 02:00 AM
This is for firefox only:
<script>
var Ajax={};
Ajax.conn=[];
Ajax.new=function(url,num)
{
if (!num)
{
num=Ajax.conn.length;
}
Ajax.conn[num]=new XMLHttpRequest();
Ajax.conn[num].onreadystatechange=function()
{
if (Ajax.conn[num].readyState==4)
{
if (Ajax.conn[num].status==200)
{
if (Ajax.conn[num].responseText)
{
alert(Ajax.conn[num].responseText);
}
else
{
Ajax.new(url,num);
}
}
}
}
Ajax.conn[num].open("get",url);
Ajax.conn[num].send(null);
}
Ajax.new("test.php");
</script>

The error exists on the line "if (Ajax.conn[num].status==200)", Error Console says: "Error: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]"

Can someone help me find an alternative or find why this is?

hayson1991
01-13-2008, 02:30 AM
I've taken off the status getter, but now it tells me that Ajax is not defined...
<script>
var Ajax={};
Ajax.conn=[];
Ajax.new=function(url,num)
{
if (!num)
{
num=Ajax.conn.length;
}
Ajax.conn[num]=new XMLHttpRequest();
Ajax.conn[num].onreadystatechange=function()
{
if (Ajax.conn[num].readyState==4)
{
if (Ajax.conn[num].responseText)
{
alert(Ajax.conn[num].responseText);
}
else
{
Ajax.new(url,num);
}
}
}
Ajax.conn[num].open("get",url);
Ajax.conn[num].send(null);
}
Ajax.new("timeouttest.php");
</script>

A1ien51
01-14-2008, 05:42 PM
num is a global varaible, so your code will fail with multiple requests.

You should set open before onreadystatechange.

If you would have searched Google for: 0x80040111 (NS_ERROR_NOT_AVAILABLE)
The first result is my blog here: http://radio.javaranch.com/pascarello/2006/02/07/1139345471027.html

Eric