Jump to content

Object Required In Ie After Xmlhttprequest


allandlewis

Recommended Posts

i have an html page, javascript page and a PHP pageeverything works fine on Firefox, Chrome and Safari however in IE i getan error "object required" on line 35 in the javascript pageline 35 is alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue);i.e. the first reference to the xmlDoc object!!i am using IE 8, and PHP 5.3 on Windows 7 Ultimatethe pages are : HTML page HTMLPage.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><script type="text/javascript" src="responsexml.js"></script><title>Untitled 1</title></head><body><form>Select a User:<select name="users" onchange="showUser(this.value)"><option value="1">Peter Griffin</option><option value="2">Lois Griffin</option><option value="3">Glenn Quagmire</option><option value="4">Joseph Swanson</option></select></form><h2><span name="firstname" id="firstname"></span> <span name="lastname" id="lastname"></span></h2><span name="job" id="job"></span><div style="text-align: left"> <span id="age_text"></span> <span name="age" id="age"></span> <span id="hometown_text" ></span> <span name="hometown" id="hometown"></span></div> </body>the JAVASCRIPT page responsexml.jsvar xmlhttp;var xmlDocfunction showUser(str){xmlhttp=GetXmlHttpObject();if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; }var url="responsexml.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);//xmlhttp.open("POST",url,true);//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xmlhttp.send(null);}function stateChanged(){if (xmlhttp.readyState==4) { if (xmlhttp.status==200) { xmlDoc=xmlhttp.responseXML; alert(document.getElementById("firstname").innerHTML); alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue); document.getElementById("firstname").innerHTML= xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue; document.getElementById("lastname").innerHTML= xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue; document.getElementById("job").innerHTML= xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue; document.getElementById("age_text").innerHTML="Age Is --====-- : "; document.getElementById("age").innerHTML= xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue; document.getElementById("hometown_text").innerHTML="<br/>From Address --====-- : "; document.getElementById("hometown").innerHTML= xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue; } }}function GetXmlHttpObject(){if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); }if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); }return null;}the PHP page responsexml.php<?php$q=$_GET["q"];$serverName = "(local)";$connectionOptions = array("Database"=>"TestPHP");$conn = sqlsrv_connect( $serverName, $connectionOptions);if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); //die('Could not connect: ' . sqlsrv_errors()); }$sql="SELECT FirstName, LastName, Age, Hometown, Job FROM [user] WHERE id = $q " ;$result = sqlsrv_query($conn, $sql, array( $q ) );header("Content-type:text/xml");header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache");$response='<?xml version="1.0" encoding="ISO-8859-1"' . chr(63) . chr(62) . " <person>";while($row = sqlsrv_fetch_array($result)) { $response = $response . "<firstname>" . $row['FirstName'] . "</firstname>"; $response = $response . "<lastname>" . $row['LastName'] . "</lastname>"; $response = $response . "<age>" . $row['Age'] . "</age>"; $response = $response . "<hometown>" . $row['Hometown'] . "</hometown>"; $response = $response . "<job>" . $row['Job'] . "</job>"; }$response = $response . "</person>";echo $response;sqlsrv_close($conn );?> any ideas ??--------------------------------------------------------------

Edited by adl
Link to comment
Share on other sites

It could be saying that anything in the expression is undefined, not necessarily the XML object. Try alerting each thing to figure out which is not defined.alert(xmlDoc);alert(xmlDoc.getElementsByTagName("hometown"));alert(xmlDoc.getElementsByTagName("hometown")[0]);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0]);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue);This line alone:xmlDoc=xmlhttp.responseXML;Is not going to set xmlDoc to an object, at that point it's just a string. You need to explicitly create a new object, I think the object you're looking for is a DOMDocument object but I could be wrong.

Link to comment
Share on other sites

It could be saying that anything in the expression is undefined, not necessarily the XML object. Try alerting each thing to figure out which is not defined.alert(xmlDoc);alert(xmlDoc.getElementsByTagName("hometown"));alert(xmlDoc.getElementsByTagName("hometown")[0]);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0]);alert(xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue);This line alone:xmlDoc=xmlhttp.responseXML;Is not going to set xmlDoc to an object, at that point it's just a string. You need to explicitly create a new object, I think the object you're looking for is a DOMDocument object but I could be wrong.
in IE alert(xmlDoc); returns object alert(xmlDoc.getElementsByTagName("hometown")); returns object alert(xmlDoc.getElementsByTagName("hometown")[0]); returns null it doesnt matter which fireld i refer to note: 1. all other browsers work fine 2. this program is identical (i think) to the w3schools program PHP - AJAX responseXML
Link to comment
Share on other sites

alert(xmlDoc.getElementsByTagName("hometown")[0]); returns nullIf that's null, that means it's not finding any hometown elements. I have essentially zero experience troubleshooting XML in IE, so I'll leave that to someone who's more proficient than I am.

Link to comment
Share on other sites

  • 2 years later...

I too facing the similar problem. i have a html file and a xml file and trying to invoke the xml data using the xmlHttpRequest, the code which i copied from the xml tutorial in w3schools with slight variations. XML file (data.xml)<data><to>Manu</to><from>Guno</from><subject>Test mail</subject><body>for a testing purpose</body></data> HTML file (inbox.html)<html><body><h1>Inbox</h1><br><script type="text/javascript"> xhttp=new XMLHttpRequest(); // for IE 7+, other browsers.. //xhttp=new ActiveXObject("Microsoft.XMLHTTP"); for IE 5, 6 xhttp.open("GET","data.xml",false); xhttp.send(); xmldoc=xhttp.responseXML; document.write(xmldoc.getElementsByTagName("to")[0].childNodes[0].nodeValue);</script></body></html> If iam using the 'new XMLHttpRequest()': in IE 8 - getting "access is denied" error for line : xhttp.open("GET","data.xml",false); in other(chrome, firefox) - working fineIf iam using the 'new ActiveXObject("Microsoft.XMLHTTP")': in IE 8 - getting "object required" error for line : document.write(xmldoc.getElementsByTagName("to")[0].childNodes[0].nodeValue); in other(chrome, firefox) - getting "object required" error for line : document.write(xmldoc.getElementsByTagName("to")[0].childNodes[0].nodeValue); so what is the problem with the IE, because as per the code given in w3schools, the first methord should work for all browser except IE 5,6. Its working for the all other but not for the IE 8.i just started learning xml with my work and IE is the only browser allowed in my system. Can anyone help me to move forward.

Link to comment
Share on other sites

Thanks for the reply, You mean i must have the admin access to machine?Could please elaborate by doing some changes in my code.

Link to comment
Share on other sites

If you just double-click on an HTML file to open it in IE, IE is running in a higher security mode and won't allow things like ajax request. You need to upload your files to a web server and access the file over HTTP.

Link to comment
Share on other sites

okfew more questions,

  • then why it is working fine in chrome and firefox but not for IE? (i tried the same in a laptop)
  • Is there any way continue with IE? (reason is, IE is the only one available in office system).so that i can continue my XML learning

Link to comment
Share on other sites

then why it is working fine in chrome and firefox but not for IE? (i tried the same in a laptop)
Because Chrome and Firefox don't use IE's security settings, they have their own.
Is there any way continue with IE?
Use a web server to upload the files to and test with. If this is going to be on a web server anyway, then you should be developing it on a web server. Otherwise, you can change IE's security settings.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...