469,306 Members | 1,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

XmlHTTPRequest and responseXML in IE

Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.

I saw one comment on the web explaining that it only parses the response
into a DOM in responseXML if the HTTP response's content type is
text/xml. That would kinda make sense, I guess, if I assumed that local
requests simply don't have a content type and so aren't parsed.

Anything else to watch out for? Is there a workaround to force IE to
always parse the response text, even if it doesn't happen to guess that
the format is XML?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 20 '05 #1
9 13641
On Fri, 25 Jul 2003 19:05:29 -0600, Chris Smith <cd*****@twu.net>
wrote:
Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.
Maybe you're issuing async requests (good) but not waiting until the
data is ready (bad).
I saw one comment on the web explaining that it only parses the response
into a DOM in responseXML if the HTTP response's content type is
text/xml. That would kinda make sense, I guess, if I assumed that local
requests simply don't have a content type and so aren't parsed.

Anything else to watch out for? Is there a workaround to force IE to
always parse the response text, even if it doesn't happen to guess that
the format is XML?


It's not a matter of "guessing" the format because the content-type
indentifies what the server is sending back. If you know better than
the server, then use responseText instead of responseXml and do
whatever you want with the data. You can also just forget about
XMLHTTP completely and use a DOMDocument object's load(<url>) method.

Regards,
Steve
Jul 20 '05 #2
Steve van Dongen wrote:
On Fri, 25 Jul 2003 19:05:29 -0600, Chris Smith <cd*****@twu.net>
wrote:
Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.


Maybe you're issuing async requests (good) but not waiting until the
data is ready (bad).


Nah, using synchronous requests.
Anything else to watch out for? Is there a workaround to force IE to
always parse the response text, even if it doesn't happen to guess that
the format is XML?


It's not a matter of "guessing" the format because the content-type
indentifies what the server is sending back. If you know better than
the server, then use responseText instead of responseXml and do
whatever you want with the data. You can also just forget about
XMLHTTP completely and use a DOMDocument object's load(<url>) method.


Well, the server will always report text/xml as far as I know, but IE
seems to not work with local files, which makes it a bit difficult to do
exploratory testing. Aside from that, I've just seen this thing start
and stop working enough that I'm nervous about trusting it to work in
production. I *think* I can trace times when it hasn't worked to errors
in the returned XML content, but since I've seen empty DOM documents
when the XML is fine in some scenarios (at least with local files), I'm
being skeptical.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 20 '05 #3
Steve van Dongen wrote:
On Fri, 25 Jul 2003 19:05:29 -0600, Chris Smith <cd*****@twu.net>
wrote:

Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.

Maybe you're issuing async requests (good) but not waiting until the
data is ready (bad).

[...snip...: Noting that the OP responded he uses sync ... ]

Could you please give an explanation of why one should prefer async over
sync?

Thanks
Stephen

Jul 20 '05 #4
On Mon, 28 Jul 2003 02:29:44 GMT, Stephen <ss*****@austin.rr.com>
wrote:
Steve van Dongen wrote:
On Fri, 25 Jul 2003 19:05:29 -0600, Chris Smith <cd*****@twu.net>
wrote:

Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.

Maybe you're issuing async requests (good) but not waiting until the
data is ready (bad).

[...snip...: Noting that the OP responded he uses sync ... ]

Could you please give an explanation of why one should prefer async over
sync?


I should clarify that my comments are intended to be taken in the
context of client-side script running in a web page. I don't believe
there are any problems with using sync requests if you are writing
script running in ASP or WSH.

I remember reading a comment by a Microsoft program manager that he
regretted that they had ever published sample code that demonstrated
sync requests. Unfortunately he never explained the reasons behind
the comment, but this is why I say to always use async...

The big thing with sending sync requests is that your page is
basically frozen from the time that the request is sent until a
response is received. When using async, the request is sent and your
callback function is called when the server responds so that you can
process the response. In the meantime, any script you have will
continue executing, the browser is free to do whatever processing it
needs to do, and your script can respond to events.

A more obscure problem is that using sync requests can potentially
lead to deadlocks though I admit that I have no idea how likely it is.
Our group was developing an application where we kept hitting
deadlocks and it was traced to the fact that the application was using
a combination of sync and async requests. I don't know the exact
details but the problem wouldn't have happened if we had used all sync
requests or all async; it was just the combination of the two types
that caused the deadlock. The browser always makes async requests;
therefore your script should make async requests as well.

Also, you have the ability to cancel an async request. OTOH with a
sync request you have no choice but to wait for it to complete (and
hope the server isn't in a bad state which causes it to never
respond).

Regards,
Steve
Jul 20 '05 #5
Steve van Dongen wrote:
On Mon, 28 Jul 2003 02:29:44 GMT, Stephen <ss*****@austin.rr.com>
wrote:

Steve van Dongen wrote:
On Fri, 25 Jul 2003 19:05:29 -0600, Chris Smith <cd*****@twu.net>
wrote:

Been banging my head against this one for some time. I'm attempting to
use XmlHTTPRequest to read an XML document from the web server and
interact with it using the DOM. So far, I've had less than perfect luck
with IE. What I've established:

- In IE, the responseXML property sometimes returns an empty document.
- It always does so when testing on local files.
- It (sometimes? always?) works fine when pages are served by a web
server.
Maybe you're issuing async requests (good) but not waiting until the
data is ready (bad).
[...snip...: Noting that the OP responded he uses sync ... ]

Could you please give an explanation of why one should prefer async over
sync?

I should clarify that my comments are intended to be taken in the
context of client-side script running in a web page. I don't believe
there are any problems with using sync requests if you are writing
script running in ASP or WSH.

... comment by a Microsoft program manager that he
regretted ...

.. your page is basically frozen ...

.. sync requests can potentially lead to deadlocks ...

The browser always makes async requests..
yes, I believe this is the nature of HTTP

...Also, you have the ability to cancel an async request...


That seems to cover a lot of bases; thanks for the thorough response...

Stephen

Jul 20 '05 #6
Steve van Dongen wrote:
The big thing with sending sync requests is that your page is
basically frozen from the time that the request is sent until a
response is received. When using async, the request is sent and your
callback function is called when the server responds so that you can
process the response. In the meantime, any script you have will
continue executing, the browser is free to do whatever processing it
needs to do, and your script can respond to events.


Are you saying that the browser is essentially single-threaded, and if I
take a long time to return from a JavaScript function, the browser can't
do anything else in that time frame? That would be a reason to use
async requests, I suppose, but I'm appalled that it is the case.

I'm not concerned about the display of the page (which is really all
auto-generated by DHTML based on the contents of the XML file I'm
loading, anyway). However, if the browser wouldn't be able to show a
menu, or close itself, or other basic functions, that would certainly be
a pain.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 20 '05 #7
Berislav Lopac wrote:
Think a little about it and all will sit perfectly. As the name implies,
XMLHTTPRequest calls a document using the HTTP protocol. When you test on
local files, you don't use the HTTP; when testing via a web server, you do.

IOW, always test with a server. It simulates the final conditions more
accurately.


Well, that would make sense if the documentation didn't explicitly say
that this class can actually issue requests over other protocols as
well. In any case, it's not the local file issue itself that concerns
me; it's the fact that there's behavior that seems to be contrary to the
documentation, and I don't feel comfortable relying on correct behavior.
I thought I'd ask here to see if anyone had a nice explanation, or
enough experience to say with confidence that this works reliably.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 20 '05 #8
On Mon, 28 Jul 2003 09:10:18 -0600, Chris Smith <cd*****@twu.net>
wrote:
Are you saying that the browser is essentially single-threaded, and if I
take a long time to return from a JavaScript function, the browser can't
do anything else in that time frame?


javascript is single threaded, and it will lock the window to input
and updates while it's processing.

Which is why you don't use sync, or sleep in any event driven
environment.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #9
Jim Ley wrote:
On Mon, 28 Jul 2003 09:10:18 -0600, Chris Smith <cd*****@twu.net>
wrote:
Are you saying that the browser is essentially single-threaded, and if I
take a long time to return from a JavaScript function, the browser can't
do anything else in that time frame?


javascript is single threaded, and it will lock the window to input
and updates while it's processing.

Which is why you don't use sync, or sleep in any event driven
environment.


Okay, point taken. This resolves my concurrency concerns, I guess, in
another thread. I assume this is true between frames, not just within a
frame? If so, then I'll definitely go switch to async requests.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marco Laponder | last post: by
4 posts views Thread by Rich Hephner | last post: by
3 posts views Thread by joker | last post: by
reply views Thread by lprisr | last post: by
4 posts views Thread by Jarod | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.