[Javascript] XMLHTTP send and receive stumper

Nick Fitzsimons nick at nickfitz.co.uk
Tue Nov 8 08:15:18 CST 2005


> Everything seemed to be going swimmingly, until I try to append the
> result. body.innerHTML+=xmlhttp.responseText works fine, but
> body.appendChild(xmlhttp.responseXML) causes the following error in
> Firefox:
>
> Error: [Exception... "Node cannot be inserted at the specified point
> in the hierarchy"  code: "3" nsresult: "0x80530003
> (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" ...]
>
> I obviously don't want to use innerHTML since everything I'm doing is
> in XML, but I haven't been able to find a good way to debug this
> error. Also, there's some other browser-compatibility errors, so if
> anyone could point to a good tutorial on sending XML and processing
> an XML response with javascript, I'd appreciate it.
>

The responseXML property holds a reference to a DOMDocument, and you can't
insert a document node into another document. If you grab the response's
documentElement, you should be able to insert it... although you can only
insert a node into its owner document, so you need to clone the node
first. Therefore, something like:

var returnedElementNode = xmlhttp.responseXML.documentElement;
document.getElementsByTagName("body")[0].appendChild(returnedElementNode.cloneNode(true));

should work. (I can't test this right now, but that's worked for me in the
past.)

Although I don't have any links to tutorials (I learnt this stuff years
ago from the docs and standards, before any tutorials were written), you
could have a look around at the MSXML docs, in the vicinity of:

<http://msdn.microsoft.com/library/en-us/xmlsdk/html/4a516883-b854-4006-849c-b4733dd89a59.asp?frame=true>

as IIRC they have some example code, and there are probably some related
articles. If you need to get a handle on the distinction between element
and document nodes, you can't do much better than go to the source:

<http://www.w3.org/DOM/>

Also, there may be some useful stuff at:

<http://developer.mozilla.org/>

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/




More information about the Javascript mailing list