Reputation: 21
I'm using JQuery's $.get function to fetch a twitter feed and display it on my site. I have no idea why it seems to not be getting any data (i.e. code inside function(d) { ... } doesn't get called). It works fine in everything else I've tried. I have also used this code before with no problems, the only thing I can think of is that it is running through https.
(Note that for the example I've removed the twitter user id from the feed url)
JS:
$.get('proxy.php?url=http://twitter.com/statuses/user_timeline/999999999.rss', function(d) {
$(d).find('item').each(function() {
var theItem = $(this);
var title = theItem.find('title').text();
var date = new Date(theItem.find('pubDate').text());
var alink = theItem.find('link').text();
// code ommitted (inserts tweet into page)
});
});
proxy.php:
<?php
// PHP Proxy
// Loads a XML from any location. Used with Flash/Flex apps to bypass security restrictions
// Author: Paulo Fierro
// January 29, 2006
// usage: proxy.php?url=http://mysite.com/myxml.xml
$session = curl_init($_GET['url']); // Open the Curl session
curl_setopt($session, CURLOPT_HEADER, false); // Don't return HTTP headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Do return the contents of the call
$xml = curl_exec($session); // Make the call
$seconds_to_cache = 300; // five mins (60 * 5)
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: maxage=$seconds_to_cache");
//header("Content-Type: text/xml"); // Set the content type appropriately
header("Content-Type: application/rss+xml");
echo $xml; // Spit out the xml
curl_close($session); // And close the session
?>
Any ideas / help would be greatly appreciated
Upvotes: 1
Views: 759
Reputation: 21
Figured it out. IE needs the content type to be set to text/xml. I changed the proxy.php script:
header("Content-Type: text/xml");
and that was all I needed.
Upvotes: 1
Reputation: 6878
EDIT:
I think I know what is going on. By default the $.get()
method has a return content type of text/html
. You are getting XML data, and immediately passing it into the jQuery
function for evaluation. All the browsers other than IE will allow you to create those XML nodes, but if IE sees an element type it does not recognize, it will fail.
It seems the solution to your problem is to explicitly say you are expecting XML from the response. See this example for how to go about this.
EDIT 2:
In further reading I've seen that there is mixed luck in getting IE to interpret the XML correctly even with the example I provided above. If you are still having problems, you can always try converting your XML to JSON on the server (see http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/ for a working class
that accomplishes this). You'll be able to consistently work with JSON on the client side, but you will need to update your success
data processing function accordingly.
Original Answer:
I would be willing to guess that the problem is IE is very happy to cache URLs. I recommend appending an additional, unused, but randomly generated query string parameter to get around this.
$.get('proxy.php?url=http://twitter.com/statuses/user_timeline/999999999.rss&rand=' + Math.floor(Math.random()*999999), function() { //omitted });
Upvotes: 0
Reputation: 24916
I had this problem before, and you're right -- you see it mostly in IE. This is the fix:
$(document).ready(function() {
$.ajaxSetup ({
cache: false
});
});
Apply that property on your page before you do the $.get() call, and it will eliminate the issue, more than likely.
Upvotes: 0