1

I scratch my head in frustration, but I can't find the answer. I'm new to Ajax and I'm trying this easy script:

Here's my code:

JAVASCRIPT:

$(document).ready(function(){    
    $("#toggle_album").click(function () {  
        $.post('backend/load_album_thumbnails.php', {  
            text: 'my string',  
                number: 23  
        }, function(xml) {  
                var timestamp = $(xml).find("time").text();
                alert(xml);
                alert(timestamp);
        });  
    });
});  

the alert(xml) returns:

     <? xml version="1.0"?>
<response status="ok">
<time>23:33:13</time>
<string>my string</string>
</response>

the alert(timestamp) returns empty

i have also tried:

timestamp = $("time",xml).text();

with the same result.

I added the extra space in the xml start tag because it dissapeared here on stackoverflow. The only reason for this error I can think of is that the return data isn't in XML format, but I can't figure out why that would be the case.

Appreciate any answers.

Mattis
  • 11
  • 1
  • I just tried your verbatim sample code and it works without problems. I also tried sending back wrong content-type along with xml and adding random whitespace at the beginning, but it still work seamlessly. Could you add some more context info (eg. server used, browser used, etc) and maybe a link to the xml file you are using? – Patonza Nov 17 '09 at 22:50
  • If you test this code in Firefox 3.5+ it will work. It will fail in IE8 however. – Horatio Alderaan Nov 17 '09 at 23:22
  • I always forget testing things in IE. Just out of curiosity, did you test this code on other browsers? – Patonza Nov 17 '09 at 23:33
  • I tested the original code in Opera 10 (Mac), Safari 4 (Mac), Chrome (XP), and FF2.0 (XP). All work as our friend Mattis would expect. IE 6, 7 and 8 all respond as described in the question. – Horatio Alderaan Nov 17 '09 at 23:59
  • Oh my you guys rock! Thanks for all the input. I did not test it in any other browser than IE7, I am afraid that my web designing days was from when IE was the way to code, and I constantly forget checking it in other browsers (even though I have them installed). I will look into it when I get to work. Thanks! – Mattis Nov 18 '09 at 07:21
  • Let us know how it goes. – Horatio Alderaan Nov 19 '09 at 22:45

1 Answers1

1

You're correct, the reason it's not working is that the return data isn't in XML format. Generally, if a web server doesn't return a text/xml Content-Type header, then certain browsers (and therefore jQuery) won't bother parsing the XML.

To get it to work, your options are:

  1. Modify your response headers to specify XML format using PHP's header() function and then, change your response so that it uses well-formed XML. The code to do this would look something like:

    header('Content-Type: text/xml; charset=UTF-8');
    echo "<?xml version=\"1.0\"?>
    <response status="ok">
      <time>$timestamp</time>
      <string>$mystring</string>
    </response>";
    
  2. Change the way you handle the response in your JavaScript code (for example, write a simple regular expression parser, although this is considered bad for your health)

  3. Change your response to something like JSON so that you can use jQuery's $.getJSON() method to send the request and parse the response to a JavaScript object (PHP has some very nice built-in JSON functions).
Community
  • 1
  • 1
Horatio Alderaan
  • 3,264
  • 2
  • 24
  • 30
  • I'd suggest to move to JSON too, it's easier to encode an parse. – Patonza Nov 17 '09 at 23:34
  • 1). Will try soon! 2). LOL! 3). Yeah I considered JSON but the script will eventually return a whole bunch of image information, which makes the XML-format ideal. – Mattis Nov 18 '09 at 07:24