By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

xmlhttprequest not working

P: n/a
Hi,

I need some help figuring this out. I cannot get the xmlhttprequest
object to return responseXML or getAllResponseHeaders. responseText
works fine, but i'd rather use xPath than regular expressions to
populate some tables.

I'm using the XMLHttpRequest object for Mozilla and the
ActiveXObject("Msxml2.XMLHTTP") for IE.

Here is the javascript:

//script to load the xml doc using xmlhttprequest or xmlhttp
if(xDoc) {
xDoc.open("GET",url,false);
xDoc.setRequestHeader("Content-Type","text/xml");

if(window.ActiveXObject){
xDoc.send("");
} else {
xDoc.send(null);
}

if (xDoc.readyState==4) {
alert(xDoc.responseText); //works fine
var xHead;
xHead = xDoc.getAllResponseHeaders();
alert(xHead); //returns null
xDoc = xDoc.responseXML.xml;
alert(xDoc.length); //returns 0
} else {
alert("Error--readyState: " + xDoc.readyState);
}
}
Any help would be great.

thanks.

Sep 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
xDoc = xDoc.responseXML.xml;


Only IE has the .xml property, also try accessing the documentElement
of xDoc.responseXML.

It might help if you show some of the xml that's beening returned. Is
it actually well formed XML.

Also if you want to use XPath Mozilla / FireFox doesn't have the
selectNodes / selectSingleNode that IE Has. This is my implymentation
of it :

http://km0ti0n.blunted.co.uk/mozXPath.xap

Hope that helps

Sep 21 '05 #2

P: n/a
ja*********@gmail.com wrote:
Hi,

I need some help figuring this out. I cannot get the xmlhttprequest
object to return responseXML or getAllResponseHeaders. responseText
works fine, but i'd rather use xPath than regular expressions to
populate some tables.

I'm using the XMLHttpRequest object for Mozilla and the
ActiveXObject("Msxml2.XMLHTTP") for IE.

Here is the javascript:

//script to load the xml doc using xmlhttprequest or xmlhttp
if(xDoc) {
xDoc.open("GET",url,false);
xDoc.setRequestHeader("Content-Type","text/xml");

if(window.ActiveXObject){
xDoc.send("");
} else {
xDoc.send(null);
}

if (xDoc.readyState==4) {
alert(xDoc.responseText); //works fine
var xHead;
xHead = xDoc.getAllResponseHeaders();
alert(xHead); //returns null
xDoc = xDoc.responseXML.xml;
alert(xDoc.length); //returns 0
} else {
alert("Error--readyState: " + xDoc.readyState);
}
}


I'm guessing these are just code fragments, as xDoc.readyState would
probably not make it to 4 in time to get to the part that //works .
(xDoc.onreadystatechange)

I'm staring at this waiting for something to jump out at me , but
I'm leaning towards the thought that there may be more to this problem
than what we can see here

I myself have already made this journey, and the result was that
I created a little library jsXMLlib.js that merged the functionality
of XMLDOM and XMLHTTP into a little javascript class. Declaring a new
connection goes like this new DCxml(yourURL,yourCallbackFunction)
and as a bonus , I can open many connections at the same time.

Perhaps if you had a sample URL , where we could see what your trying to
do in it's totality.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 21 '05 #3

P: n/a
Well, I tried the documentElements property too, and that didn't work
either. here is some more fun. The status and statusText properties
don't return:

....
if (xDoc.readyState==4) {
alert(xDoc.statusText); //returns "unknown"
alert(xDoc.responseText); //works fine
....

also, I did have this code working in IE when I used the DomDocument3.0
active X object. however, in xmlhttp it doesn't work (well, the
responseText method works).

here is a node of the XML file. i haven't edited this since it worked
fine before:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<?META http-equiv="Content-Type" content="text/xml"?>
<product>
<item>
<sSpec_Number>[deleted]</sSpec_Number>
<EPM_Product_Name>[deleted]</EPM_Product_Name>
<Architecture>[deleted]</Architecture>
<L2_Cache>512KB</L2_Cache>
<Clock__Speed>3</Clock__Speed>
<Front_Side_Bus_Speed>800</Front_Side_Bus_Speed>
<Other_Technologies>[deleted]Other_Technologies>
</item>
</product>

yeah, that selectNodes thing is a drag.

Thanks for your help.

km0ti0n wrote:
xDoc = xDoc.responseXML.xml;


Only IE has the .xml property, also try accessing the documentElement
of xDoc.responseXML.

It might help if you show some of the xml that's beening returned. Is
it actually well formed XML.

Also if you want to use XPath Mozilla / FireFox doesn't have the
selectNodes / selectSingleNode that IE Has. This is my implymentation
of it :

http://km0ti0n.blunted.co.uk/mozXPath.xap

Hope that helps


Sep 21 '05 #4

P: n/a
Indeed, Ian is right. You need to create a handler for the requests.
that you can resue time and time again. It's really simple to generate
a cross browser compatable one. Again there's a link to one on the url
I posted (XHConn) in the Acknolegments,

Sep 21 '05 #5

P: n/a
Unfortunately, I cannot post the whole script. However, the piece that
I posted above is where the problem is. when I built the prototype
(which worked fine), I did it using new ActiveXObject(DomDocument3.0)
instead of new XMLHttpRequest (or new ActiveXObject(msxml2.XMLHTTP) and
they don't work the same at all, apparently. Since the implimentation
needs to be cross-browser, I'm kinda stuck.

hopefully converting the selectNodes xPath stuff to xmldom won't be so
frustrating.

Thanks.

Sep 21 '05 #6

P: n/a
Use try / catch to test which one of the objects you need to create.

var xhr;
try{ xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e){/*FF fails so use*/ xhr = XMLHttpRequest();}

But theres a link to a fully functioning example off
http://km0ti0n.blunted.co.uk/mozXPath.xap go to the bottom of the page
and click XHConn.

Sep 21 '05 #7

P: n/a
ja*********@gmail.com wrote:
Unfortunately, I cannot post the whole script. However, the piece that
I posted above is where the problem is. when I built the prototype
(which worked fine), I did it using new ActiveXObject(DomDocument3.0)
instead of new XMLHttpRequest (or new ActiveXObject(msxml2.XMLHTTP) and
they don't work the same at all, apparently. Since the implimentation
needs to be cross-browser, I'm kinda stuck.

hopefully converting the selectNodes xPath stuff to xmldom won't be so
frustrating.

Thanks.


If you go here http://www.drclue.net/projects/jsDHTMLlib/
and download the jsDHTMLlib.zip, you'll find the jsXMLlib.js
contained in the zip.

It works on IE,NS,*Opera*,Firefox, and just about anything else that
has the support or a way to fake it.

If nothing else , you can look over what I did and clone the
parts you neeed.
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 21 '05 #8

P: n/a
ja*********@gmail.com wrote:
I need some help figuring this out. I cannot get the xmlhttprequest
object to return responseXML or getAllResponseHeaders.
It appears that you create the object and send the request, but you don't
want for it to come back.
if (xDoc.readyState==4) {


Rather than this being immediately after the send call, you need to wait for
your object to trigger an onreadystate change event, then check where it's
at.

To simplify the process, you may want to consider using a generalized lib
like the one found at my Ajaxtoolbox.com site (see sig).

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.