It's part of an AJAX segment of code that should retrieve information from a publisher program that I'm a part of. It gets called from the page a dynamically produced number of times, but each time it's called like this:

sendRequest(url1, 1)
sendRequest(url2, 2)
sendRequest(url3, 3) etc.

Here are the relevant segments of code in javascript (i'm not going to include the creation of the XML object to begin with, i know that the problem is not there):

function processRequest(num)
{
if(xmlHttp.readyState == 4){
var response = xmlHttp.responseXML;
alert(num);
x = response.documentElement.getElementsByTagName("productResponse");
xx=x[0].getElementsByTagName("name");
txt=xx[0].firstChild.nodeValue;

theId="serverResults" + num;
e = document.getElementById(theId);
e.innerHTML=txt;
}

}

function sendRequest(url, number)
{
xmlHttp.open('get', 'mediatorscript.php?x=' + url);
xmlHttp.onreadystatechange = function() { processRequest(number) }
xmlHttp.send(null);
}

I know, or at least I think, that the problem is that the function() { processRequest(number)} is not acting how I want it to. Can someone tell me what I should be doing?

    Try this out, I changed a few subtle things.

    function sendRequest(url, number) {
        xmlHttp.open('get', 'mediatorscript.php?x=' + url, true);
        xmlHttp.onreadystatechange = processRequest(number);
        xmlHttp.send();
    }
    
    function processRequest(num) {
    
    if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
        var response = xmlHttp.responseXML;
        alert(num);
        x = response.documentElement.getElementsByTagName("productResponse");
        xx=x[0].getElementsByTagName("name");
        txt=xx[0].firstChild.nodeValue;
    
        theId="serverResults" + num;
        e = document.getElementById(theId);
        e.innerHTML=txt;
    }
    }
    

    Now I doubt that will work do to "xx=x[0].getElementsByTagName("name")" not being proper syntax. If you wish to get a child Element of "productResponse" use the childNodes[x].

    So assuming it is the first child under "productResponse" it should be "xx=x[0].childNode[0]". Now some browsers have issues if there is white space between the tags. But as you are writing the XML make sure there is no white space.

    Of course why you wrote your XML with an extra child elements is a little odd considering you only need one element. Is there a reason the contents couldn't be put in "productResponse" and save yourself the hassle of parsing through an extra child element.

      Okay, so the alert isn't happening when it's inside the if(xmlHttp.readyState==4) clause, but it does work if it comes right before the if clause just in the processRequest function. Does this mean that the url that i'm getting the xml from is invalid? Actually what does this mean 😉?

      Oh, and I'm not writing the XML.

      Thanks!

        Finding the errors in AJAX is a bit of a trick. I have had to develop custom debugging each time I have used it.

        First I recommend using Firefox as its javascript error reporting is very good. This may tell you if there is an error in your javascript.

        Next if you're not getting the alert it means that the readystate and status are not coming back as 4 and 200, respectively. Which could mean nothing is being returned by "mediatorscript.php".

        So try using your browser go directly to the "mediatorscript.php" with the GET parameters set in the URL and see if it outputs any XML. If its working correctly you will see XML with the data that it would normally be sending to your AJAX function.

        Oh, and I'm not writing the XML

        Your writing the code in "mediatorscript.php", so via the code in that page you are writing the XML.

          Have a diagnostic alert before that test
          alert("readyState = " + xmlHttp.readyState + "\nstatus=" + xmlHttp.status)

          and see what that gives. Note, too, that any response status in the 200..299 range indicates "success".

            Weedpacket;10909125 wrote:

            Have a diagnostic alert before that test
            alert("readyState = " + xmlHttp.readyState + "\nstatus=" + xmlHttp.status)

            and see what that gives. Note, too, that any response status in the 200..299 range indicates "success".

            I tried that, so i made my processRequest method look like this:

            function processRequest(num)
            {
            alert("hi");
            alert(xmlHttp.readyState);
            alert(xmlHttp.status);
            alert("readyState = " + xmlHttp.readyState + "\nstatus=" + xmlHttp.status);
            if (xmlHttp.readyState == 4){
            var response = xmlHttp.responseXML;
            x = response.documentElement.getElementsByTagName("productResponse");
            xx=x[0].getElementsByTagName("name");
            txt=xx[0].firstChild.nodeValue;

            theId="serverResults" + num;
            e = document.getElementById(theId);
            e.innerHTML=txt;
            }

            }

            the alert("hi") went off and then the number 1 appeared then hi and 1 again.
            In this particular case the sendRequest function was called twice, first with (url, 1) and then (url, 2)

            also:
            "
            So try using your browser go directly to the "mediatorscript.php" with the GET parameters set in the URL and see if it outputs any XML. If its working correctly you will see XML with the data that it would normally be sending to your AJAX function.
            "

            I did that, and the mediatorscript is outputing the XML just fine. All it's really doing is echoing a file get contents of the url to get around the domain origin browser security

              That means the readystate is 1 and that means it hanging up before it sends the data to "mediatorscript.php".

              Are you getting any javascript errors?

                Krik;10909143 wrote:

                That means the readystate is 1 and that means it hanging up before it sends the data to "mediatorscript.php".

                Are you getting any javascript errors?

                Error: uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost/clashem/shopzilla/shopzilla.js :: processRequest :: line 46" data: no]

                that line is the: alert(xmlHttp.status);

                surrounded by:

                function processRequest(num)
                {
                alert("hi");
                alert(xmlHttp.readyState);
                alert(xmlHttp.status);
                alert("readyState = " + xmlHttp.readyState + "\nstatus=" + xmlHttp.status);

                  No status will not be returned until readystate 3. So remove that line and see what happens.

                    Right, so the fix seemed to be changing the onreadystate line to this:
                    xmlHttp.onreadystatechange = function() {processRequest(number);}
                    I guess that if you don't call it like this, it actually makes onreadystatechange = to the function (even though the function doesn't return anything). I'm not really sure, but that seemed to have been causing the problem, and the xmlHttp.readystate no longer hangs at 1 and gets to 4.

                    Now I have another issue though, the responseXML is null. I've set the header content type in the mediatorscript so that doesn't seem to be the problem, and if I do alert(xmlHttp.responseText), everything seems to be fine.
                    I've opened mediatorscript with the url that the page is calling it with, and the output is in xml tree format in firefox. Any suggestions?

                    mediatorscript looks like this:
                    <?php
                    header("Content-type: text/xml");
                    $url= $_GET['x'];
                    $url=preg_replace('#*#', '+', $url);
                    echo file_get_contents("$url");
                    ?>

                    Because php has problems getting + signs from the url, i just converted them to * before hand in generating the function call.

                      Write a Reply...