/    Sign up×
Community /Pin to ProfileBookmark

HTTP Request – responseXML Problem

I’m having some trouble using the responseXML property.

This function tries to load an external XHTML file and copy the contents of its body (all the child nodes and their descendants) into a content div in the main page (identified by pDestId):

[code]function pLoad(pDestId, pSrcFileName)
{

var request = false; // HTTP request object

var destNode;
var srcNode;

// Try creating HTTP request object
try { request = new XMLHttpRequest(); } // conforming browsers
catch (e)
{
try { request = new ActiveXObject(“Msxml2.XMLHTTP”); } // MSIE
catch (e)
{
try { request = new ActiveXObject(“Microsoft.XMLHTTP”); }
catch (e)
{
alert(“Error: HTTP request object could not be created. Your browser must not support AJAX.”);
return false;
}
}
}

// Open file
request.open(“GET”, pSrcFileName, true);
request.send(null);

// Set state change method
request.onreadystatechange = function()
{
if ( request.readyState == 4 &&
request.status == 200 )
{
// Replace content
[b][i]srcNode = document.importNode(request.responseXML.documentElement.getElementsByTagName(“body”)[0], true);[/i][/b]
destNode = document.getElementById(pDestId);

removeChildren(destNode);
copyChildren(destNode, srcNode);

}
}

}[/code]

The above code works fine when I try it locally (in every browser but IE), but when I upload it to a server it doesn’t work at all. The problem seems to be only with the bold line above.

Everything works perfectly if I replace it with something like this (so, creating my own node tree instead of reading it from a file):

[code]

{
// Replace content
[b][i]srcNode = document.createElement(“div”);
srcNode.appendChild(document.createTextNode(“stuff”));[/i][/b]
destNode = document.getElementById(pDestId);

removeChildren(destNode);
copyChildren(destNode, srcNode);

}

[/code]

So it seems like it’s a problem with the “responseXML” line.

Also, my XHTML is being served as “text/html”, so could that be causing this problem?

I’ve got an equivalent function which uses innerHTML, and it works in every browser I’ve tested it in. I’m just trying to write something using standard DOM methods instead.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Jul 31.2008 — external file as in another domain? JavaScript can not talk to other domains.


The line: "but when I upload it to a server it doesn't work at all" does not tell us what the error is. What is the error message?

Eric
Copy linkTweet thisAlerts:
@cgishackJul 31.2008 — have you used FireBug to see if its showing you the exact error?
Copy linkTweet thisAlerts:
@rnd_meJul 31.2008 — 

I've got an equivalent function which uses innerHTML, and it works in every browser I've tested it in. I'm just trying to write something using standard DOM methods instead.[/QUOTE]

why fix what ain't broken? what browser do you need to use that doesn't support .innerHTML?
Copy linkTweet thisAlerts:
@Koiby25authorJul 31.2008 — external file as in another domain? JavaScript can not talk to other domains.[/QUOTE]

Not another domain. I just meant from one file to another.

The line: "but when I upload it to a server it doesn't work at all" does not tell us what the error is. What is the error message?[/QUOTE]

IE gives the error "Error: 'request.response.documentElement' is null or not an object'".

I tried Firebug just now and got the error "request.responseXML is null".

And, to my surprise, everything works just fine in Opera.
Copy linkTweet thisAlerts:
@Koiby25authorAug 01.2008 — Read this:

http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html[/QUOTE]


Awesome, thanks!

I basically just changed the extensions of the files I was loading from .html to .xhtml, and added this before the doctype declaration:

[CODE]<?xml version="1.0" encoding="UTF-8"?>[/CODE]

Now it works in all browsers but IE, which gives the same error. I'll probably use innerHTML for just IE, unless I can resolve that problem.
Copy linkTweet thisAlerts:
@A1ien51Aug 01.2008 — You could be cached. If you open up the "document" directly in the browser, does IE render it correctly?

Eric
Copy linkTweet thisAlerts:
@Koiby25authorAug 02.2008 — You could be cached. If you open up the "document" directly in the browser, does IE render it correctly?

Eric[/QUOTE]


I'm not quite sure what you mean by that. Which "document"? and what do you mean by "directly"?
×

Success!

Help @Koiby25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...