Forum Moderators: open

Message Too Old, No Replies

XMLHttpRequest object not seen in handler

         

OrlandoM

6:31 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



Hi all!
I've got a problem with handler of XMLHttpRequest object, it doesn't see this object. I have read a lot of articles and a book on Ajax. Wrote code according to book

My JS snippet

function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// Массив версий для старых браузеров IE
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP');

for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
try
{
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}

catch (e) {}
}
}

if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
alert ("xmlHttp successfuly created!");
return xmlHttp;
}

function requestVendors(sel)
{
var category = sel.options[sel.selectedIndex].value;
var xmlHttp = createXmlHttpRequestObject();
xmlHttp.open("GET", "http://wellsend.wood-mi.com.ua/ajax/getList.php?sub_id="+category, true);
xmlHttp.onreadystatechange = fillSelectList;
xmlHttp.send(null);
}

function fillSelectList()
{
var sbox = document.getElementById("vendor");
if(xmlHttp.readyState == 4) //Bang! Error occures here
{
if(xmlHttp.status == 200)
{
var opts = xmlHttp.responseXML.getElementsByTagName("option");
for (var i = 0; i < opts.length; i++) {
var op = new Option(
unescape(opts[i].getElementsByTagName('vendor').item(0).firstChild.data),
unescape(opts[i].getElementsByTagName('id').item(0).firstChild.data),
false, false);
sbox.options[sbox.length] = op;
}
}
}
}

Tested in IE, Opera the same bug in both browsers (Error console of Opera says "Undefined variable xmlHttp").
Help, please

OrlandoM

8:05 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



Solved, I had to declare and initialize xmlHttp as global variable. I thought that there is no need to pass object to handler.