www.webdeveloper.com
Results 1 to 9 of 9

Thread: HTTP Request - responseXML Problem

  1. #1
    Join Date
    Jul 2008
    Posts
    5

    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
    			srcNode = document.importNode(request.responseXML.documentElement.getElementsByTagName("body")[0], true);
    			destNode = document.getElementById(pDestId);
    
    			removeChildren(destNode);
    			copyChildren(destNode, srcNode);
    
    		}
    	}
    	
    }
    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
    	srcNode = document.createElement("div");
    	srcNode.appendChild(document.createTextNode("stuff"));
    	destNode = document.getElementById(pDestId);
    
    	removeChildren(destNode);
    	copyChildren(destNode, srcNode);
    
    }
    
    ...
    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.

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    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

  3. #3
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    have you used FireBug to see if its showing you the exact error?

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Koiby25

    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.
    why fix what ain't broken? what browser do you need to use that doesn't support .innerHTML?

  5. #5
    Join Date
    Jul 2008
    Posts
    5
    Quote Originally Posted by A1ien51
    external file as in another domain? JavaScript can not talk to other domains.
    Not another domain. I just meant from one file to another.

    Quote Originally Posted by A1ien51
    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?
    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.

  6. #6
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579

  7. #7
    Join Date
    Jul 2008
    Posts
    5
    Quote Originally Posted by A1ien51
    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"?>
    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.

  8. #8
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You could be cached. If you open up the "document" directly in the browser, does IE render it correctly?

    Eric

  9. #9
    Join Date
    Jul 2008
    Posts
    5
    Quote Originally Posted by A1ien51
    You could be cached. If you open up the "document" directly in the browser, does IE render it correctly?

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles