SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Xml gets returned but my request.reponse object has no properties!?

    Hi,

    I know this is technically javascript but i thought the expertise needed to answer this question are more likely to be found here:


    I have an Ajax request that sends a request to the server...the server is responding in xml....heres the php that builds and echos the xml:

    Code PHP:
    //Send some headers to keep the user's browser from caching the response.
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    header("Content-Type: text/xml; charset=utf-8");
       $xml = '<?xml version="1.0" ?><totals>';
       $xml .= '<total>'. $total . '</total>';
    $xml .= '<deletethis>' . $deleteit . '</deletethis>';
    $xml .= '</totals>';
    echo $xml;

    So this seems fine to me...i can't spot any problems ( i lifted it right from another script that works). The Firebug console shows this in its response panel:

    Code XML:
    <?xml version="1.0" ?><totals><total>0</total><deletethis>175</deletethis></totals>

    looks ok yeah?

    Now if i do:

    Code JavaScript:
    function deleteIt() {
    	 var xmlDoc = request.responseXML;

    in the handling function i get an error saying xmlDoc has no properties. Why? i just cant see why! any ideas?

    I don't think i need to post any other code as everything else if working

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function deleteIt(request) {
         var xmlDoc = request.responseXML;
    }
    ?

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No i've never needed to pass a reference before ( and yes ive got the closing bracket!)....i also tried it and firebug console alerts back that this:

    deleteIt(undefined)

    if i send back text then it works but i need xml really. Its weird because everything else works.....the php script correctly sends back some XML ( posted above) then deleteIt() function runs....i think it must be that its not recoginising the reponse as xml...perhaps the XML isnt formed properly or i dont have the correct headers sending back form php...

    I've posted some more code...i don't know if anyone can spot my mistake ( sorry about the quirky naming conventions!):

    AJAX

    Code JAVASCRIPT:
    //sends a request to update the total. if the user has updated the total to be 0 then send a delete request      
    function updateTotal(textInput) {
        createRequest(); 
        var itemID = textInput.name;
        var newquantity = textInput.value;
        if(newquantity == '') {}
        else if(newquantity == 0) {
            var url = "updatebasket.php?deleteitid=" + escape(itemID);
            request.open("GET", url, true);
            request.onreadystatechange = deleteIt;
        } else {
            var url = "updatebasket.php?changequantityID=" + escape(itemID) + "&newquantity=" + escape(newquantity);
            request.open("GET", url, true);
            request.onreadystatechange = updateIt;
        }
        request.send(null);
    }
     
    //handles the response from updateTotal and DOM'S in the new total
    function updateIt() {
      var yeah = request.responseText;
      if(request.readyState == 4) {
        var total = document.getElementById("totalyeah");
        var totaltotal = total.firstChild.nodeValue;
        replaceText(total,yeah);
      }
    }
     
     
    //handles the response from updateTotal if the user has set the total to be 0
    function deleteIt(request) {
    	 var xmlDoc = request.responseXML;
     
     
        if(request.readyState == 4) {
     
            var newTotal = xmlDoc.getElementsByTagName("total");
            var newTotalValue = newTotal[0];
       		var total = document.getElementById("totalyeah");
       		replaceText(total,newTotalValue);
       		var deleteThis = xmlDox.getElementsByTagName("delete_row");
       		var deletethisValue = deleteThis[0];
        }
    }

    and the php script ( ive always used these headers and versions of this script so i.m sure this cant be the problem)

    Code PHP:
    //Send some headers to keep the user's browser from caching the response.
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    header("Content-Type: text/xml; charset=utf-8");
       $xml = '<?xml version="1.0" ?><totals>';
       $xml .= '<total>'. $total . '</total>';
    $xml .= '<deletethis>' . $deleteit . '</deletethis>';
    $xml .= '</totals>';
     
     
     echo $xml;

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post

    Code JAVASCRIPT:
    function deleteIt(request) {
    	 var xmlDoc = request.responseXML;
     
     
        if(request.readyState == 4) {
    There is no responseXML, if readyState isn't 4.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    urggh...sorry raffles...fixed it....the problem was lower down with this line:

    var deleteThis = xmlDoc.getElementsByTagName("delete_row");

    The xml sent back is in a tag called deletethis....i wrote the javascript stuff first obviously and just wrote the wrong tag name in the xml...schoolboy stuff!

    I was sniffing aorund higher up in the script because i assumed there was something wrong with the reponse object!

    Thanks for your time

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh yeah thanks for pointing that out kyberfabrikken

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    If you are using prototype framework you need to use

    PHP Code:
    header("Content-Type: application/xml;"); 
    Not

    PHP Code:
    header("Content-Type: text/xml; charset=utf-8"); 

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Michael,

    Yes funnily enough i am using Prototype now but when i started coding this i wasn't so it's just straightfoward Ajax. I love the fact that Prototpye allows you to make a request using one line of code!


Bookmarks

Posting Permissions

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