Jump to content

[SOLVED] Javascript can't get html code from a xml request


rReLmy

Recommended Posts

Hi

 

I've got a problem to become html from a xml answer.

What comes out is only the part before the first html Tag.

 

I created my Script from this 2 Tutorials

http://developer.mozilla.org/en/docs/AJAX:Getting_Started

http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html *XML part*

 

why does the js cut all after the first html Tag?

I can't understand it...

 

Here is the sample page http://www.rrelmy.ch/ajax/xml

FF Extension says that the response coming clean.

 

the js

<script type="text/javascript">
function makeRequest(site) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up  Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest, site); };
        httpRequest.open('GET', 'ajax.php?site='+site, true);
        httpRequest.send('');

    }

    function alertContents(httpRequest, site) {

        try {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    //alert(httpRequest.responseText);
                    //document.getElementById('content').innerHTML = httpRequest.responseText;
                    gen_site(httpRequest, site);
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
        catch( e ) {
            alert('Caught Exception: ' + e.description);
        }

    }
    function gen_site(xml, site) {

var html = xml.responseXML.getElementsByTagName('replace');

//alert(html.length);

for (var i=0;i<html.length;i++)
{
	//alert(getNodeValue(html[i],'id'));
	document.getElementById(getNodeValue(html[i],'id')).innerHTML = getNodeValue(html[i],'html');
}

    	set_navi(site);
    }

    function set_navi(site) {
    	document.getElementById('n_home').setAttribute("class", "", 0);
    	document.getElementById('n_scripts').setAttribute("class", "", 0);
    	document.getElementById('n_'+site).setAttribute("class", "on", 0);
    }

function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
//return obj.getElementsByTagName(tag)[0].firstChild.innerHTML;
}

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}

</script>

 

sample ajax answer

<?xml version="2.0" ?>
<root>
<replace>
   <id>content</id>
   <html>The Content <p>wont be shown, bliblablub</p></html>
</replace>
<replace>
   <id>panel</id>
   <html>Something Other<b>Blub</b></html>
</replace>
</root>

Link to comment
Share on other sites

Thx for your answer.

 

After Hours of searching and trying i've found the solution!

Really :)

 

You have to put the CDATA thing around the "html"

 

A sample

<root>
<replace>
   <id>content</id>
   <html><![CDATA[The Content <p>wont be shown, bliblablub</p>]]></html>
</replace>
<replace>
   <id>panel</id>
   <html><![CDATA[something Other<b>Blub</b>]]></html>
</replace>
</root>

 

The JS Code in my first Post should work with it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.