views:

526

answers:

3

I'm using jQuery to setup an Ajax request that grabs an XML feed from a PHP script and then parses some information out of the feed and inserts it into the DOM. It works fine in Firefox; however, in Chrome, I am getting an empty string for the title element.

Here's the basic setup of the Ajax request:

$.get('feed.php', function(oXmlDoc) {
  $(oXmlDoc).find('entry').each(function() {
    $(this).find('title').text();
    $(this).find('id').text();
    // do other work...
  }); 
});

For what it's worth, here's the PHP script that's grabbing data from the feed. I'm using cURL because I'm making the request across domains (and because it was a quick and dirty solution for the problem at hand).

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $str_feed_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($curl);
curl_close($curl);

echo $xml;

The XML data is being returned correctly and I can see the values of the sibling nodes in Chrome (such as ID), but, for whatever reason, I continue to get an empty string for the title node.

Edit: As requested, here's a fragment of the relevant XML:

<entry>
    <id>http://TheAddress.com/feed/01&lt;/id&gt;
    <title type="text">The Title of the Post</title>
    <author><name>Tom</name></author>
    <published>2009-11-05T13:46:44Z</published>
    <updated>2009-11-05T14:02:19Z</updated>
    <summary type="html">...</summary>
</entry>
+2  A: 

The page you have in the example XML has an HTML entity in the title. This can cause issues. Here is a blog post I found on this issue.

I wonder if the same goes for other special characters...

The home page title looks like this:

<title>The Address Hotels + Resorts</title>
Buggabill
+1  A: 

I haven't tried it, but make sure that the xml is returned with the correct content type (text/xml). You can also set the dataType to xml at the jQuery.ajax(options).

kgiannakakis
I ended up needing to specify the Content-Type as rss+xml in the PHP script. $.get() ended up working just fine after that.
Tom
A: 

Hi! I have the same problem. It seems than Chrome don't handle html tag in ajax. try changing "title" to "booktitle" in the XML and in the JS.

Felipe Ortega