CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   ResponseXML (http://www.codingforums.com/showthread.php?t=180154)

Jon W 10-21-2009 08:55 AM

ResponseXML
 
Hello, I've been trying to figure out this problem out for almost 3 hours now and I've looked all over the web and I can't really find any good tutorial help on this.

The following script works fine with request.responseText;

Code:

<html>
<head><title></title>
<script type="text/javascript" language="javascript">
                        var request;
        function requestViews() {
                try {
                //takes care of all browser but IE       
                        request = new XMLHttpRequest();
                }catch(e) {
                        try {
                                request = new ActiveXObject("Msxml2.XMLHTTP");
                        }catch(e) {
                                try {
                                        request = new ActiveXObject("Microsoft.XMLHTTP");
                                }catch(e) {
                                        alert("Your browser doesn't seem to support XMLHTTP");
                                }
                        }
                }
               
                request.onreadystatechange = function() {
                        if(request.readyState == 4) {
                               
                                if (request.status == 200 || request.status == 304) {
                               
                                        var xmlDoc = request.responseXML;
                document.getElementById('totalView').innerHTML = xmlDoc.getElementByTagName('views')[0].childNodes[0].nodeValue;
                document.getElementById('short').innerHTML = xmlDoc.getElementByTagName('short')[0].childNodes[0].nodeValue;
                                       
                                       
                                }
                                else
                               
                                {
                                        var xmlDoc = request.responseXML;
                document.getElementById('totalView').innerHTML = xmlDoc.getElementByTagName('views')[0].childNodes[0].nodeValue;
                document.getElementById('short').innerHTML = xmlDoc.getElementByTagName('short')[0].childNodes[0].nodeValue;
                                       
                                }       
                        }
                        else if (request.readyState <= 0) {
                               
                                document.getElementById('totalViews').innerHTML = "<img src=\"/images/loader2.gif\" />";
                               
                        }
                }
                var title = "How to install updates in Vista";
                var queryString = "?title="+title;
                request.open("GET", "serverTime.php"+queryString, true);
                request.send(null);
               
                setTimeout("requestViews()", 1000);
               
        }
</script>
</head>
<body onload="requestViews();">
<div id="short"></div>
<div id="totalView"></div>
</body>
</html>

PHP CODE:

PHP Code:

<?php
header
('Content-Type: text/xml');

//A date in the past
$title $_GET['title'];
    echo 
"<tutorial>";
        echo 
"<views id=\"1\">".$title."</views>";
        echo 
"<short id=\"2\">".$title."</short>";

    echo 
"</tutorial>";
?>

But with responseXML I got a error message from IE and other browser don't display anything.

What am I doing wrong here?


Error Message:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Timestamp: Wed, 21 Oct 2009 07:53:16 UTC


Message: Object doesn't support this property or method
Line: 28
Char: 3
Code: 0


Message: Object doesn't support this property or method
Line: 28
Char: 3
Code: 0

Message: Object doesn't support this property or method
Line: 28
Char: 3
Code: 0

barkermn01 10-21-2009 01:08 PM

Your Not Telling AJAX its an XML File you missing the XML Header go lookup on XML Standards,

echo "<?xml version="1.0" encoding="iso-8859-1"?>";

and make sure you echo it coz that header in php buggs if short tags are allowed

Jon W 10-21-2009 06:19 PM

Okay this is something I have already tried before. I still get a error back from JavaScript.

ckeyrouz 10-21-2009 06:22 PM

There is a typing mistake.
You should replace:
Code:

getElementByTagName
with
Code:

getElementsByTagName

Jon W 10-21-2009 07:10 PM

As much as I would like to say that work, it did not. I'm still getting the same error.

ckeyrouz 10-21-2009 07:15 PM

Can you please insert these two lines:
Code:

alert(request.parseError.reason)
alert(request.parseError.srcText)

Directly after this line:
Code:

if(request.readyState == 4) {
And send me back the result of the alert messages.

Jon W 10-21-2009 07:30 PM

I put that in my code and it isn't popping up with a error or anything. Not to sure why though.

ckeyrouz 10-21-2009 07:34 PM

No problem for that this is specific for IE I think and it does not work on other browsers anyway replace the code that I have posted in the previous reply with the following;
Code:

alert(request.responseText);
And feed me back with the result, please.

Jon W 10-21-2009 07:45 PM

<?xml version="1.0"?><tutorial><views>164</views><short>There is some updates that you will have to make sure that you install. So if you are unsure how to do this then this tutorial will explain step by step how to do that.</short></tutorial>

XML is coming out fine. I just simply don't get it.

ckeyrouz 10-21-2009 07:51 PM

ok now would you try this:
Code:

alert(request.responseXML.xml)

Jon W 10-21-2009 07:54 PM

undefined

ckeyrouz 10-21-2009 07:57 PM

try adding the CDATA section to the short tag as follows:
Code:

echo "<short id=\"2\"><![CDATA[".$title."]]></short>";

Jon W 10-21-2009 08:01 PM

The alert stays the same. undefined

ckeyrouz 10-21-2009 08:04 PM

Do you have a link to the page?

Jon W 10-21-2009 08:05 PM

Code:

<html>
<head><title></title>
<script type="text/javascript" language="javascript">
                        var request;
        function requestViews() {
                try {
                //takes care of all browser but IE       
                        request = new XMLHttpRequest();
                }catch(e) {
                        try {
                                request = new ActiveXObject("Msxml2.XMLHTTP");
                        }catch(e) {
                                try {
                                        request = new ActiveXObject("Microsoft.XMLHTTP");
                                }catch(e) {
                                        alert("Your browser doesn't seem to support XMLHTTP");
                                }
                        }
                }
               
                request.onreadystatechange = function() {
                        if(request.readyState == 4) {
                                alert(request.responseText);
                                if (request.status == 200 || request.status == 304) {
                               
                                        var xmlDoc = request.responseXML;
                document.getElementById('totalView').innerHTML = xmlDoc.getElementsByTagName('views')[0].childNodes[0].nodeValue;
                document.getElementById('short').innerHTML = xmlDoc.getElementsByTagName('short')[0].childNodes[0].nodeValue;
                                       
                                       
                                }
                                else
                               
                                {
                                        var xmlDoc = request.responseXML;
                document.getElementById('totalView').innerHTML = xmlDoc.getElementsByTagName('views')[0].childNodes[0].nodeValue;
                document.getElementById('short').innerHTML = xmlDoc.getElementsByTagName('short')[0].childNodes[0].nodeValue;
                                       
                                }       
                        }
                        else if (request.readyState <= 0) {
                               
                                document.getElementById('totalViews').innerHTML = "<img src=\"/images/loader2.gif\" />";
                               
                        }
                }
                var title = "How to install updates in Vista";
                var queryString = "?title="+title;
                request.open("GET", "serverTime.php"+queryString, true);
                request.send(null);
       
               
        }
</script>
</head>
<body onload="requestViews();">
<div id="short"></div>
<div id="totalView"></div>
</body>
</html>

Not to sure what exactly happened but this seems to be working now.


All times are GMT +1. The time now is 07:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.