Connecting Tech Pros Worldwide Forums | Help | Site Map

xmlhttprequest not working

jason.lucey@gmail.com
Guest
 
Posts: n/a
#1: Sep 21 '05
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.


km0ti0n
Guest
 
Posts: n/a
#2: Sep 21 '05

re: xmlhttprequest not working


[color=blue]
> xDoc = xDoc.responseXML.xml;[/color]

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

Dr Clue
Guest
 
Posts: n/a
#3: Sep 21 '05

re: xmlhttprequest not working


jason.lucey@gmail.com wrote:[color=blue]
> 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);
> }
> }[/color]

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
--.
jason.lucey@gmail.com
Guest
 
Posts: n/a
#4: Sep 21 '05

re: xmlhttprequest not working


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:[color=blue][color=green]
> > xDoc = xDoc.responseXML.xml;[/color]
>
> 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[/color]

km0ti0n
Guest
 
Posts: n/a
#5: Sep 21 '05

re: xmlhttprequest not working


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,

jason.lucey@gmail.com
Guest
 
Posts: n/a
#6: Sep 21 '05

re: xmlhttprequest not working


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.

km0ti0n
Guest
 
Posts: n/a
#7: Sep 21 '05

re: xmlhttprequest not working


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.

Dr Clue
Guest
 
Posts: n/a
#8: Sep 22 '05

re: xmlhttprequest not working


jason.lucey@gmail.com wrote:[color=blue]
> 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.
>[/color]

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
--.
Matt Kruse
Guest
 
Posts: n/a
#9: Sep 22 '05

re: xmlhttprequest not working


jason.lucey@gmail.com wrote:[color=blue]
> I need some help figuring this out. I cannot get the xmlhttprequest
> object to return responseXML or getAllResponseHeaders.[/color]

It appears that you create the object and send the request, but you don't
want for it to come back.
[color=blue]
> if (xDoc.readyState==4) {[/color]

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


Closed Thread


Similar JavaScript / Ajax / DHTML bytes