Note that there are some explanatory texts on larger screens.

plurals
  1. POSuccessful AJAX command not returning new SimplePie generated HTML (from RSS Feed) to the browser
    primarykey
    data
    text
    <p>I'm working on creating an AJAX powered RSS Feed. It is a single pane similar to Google Reader with a list of feeds on the left and the feed content on the right. When you click on any of the feeds this will trigger an ajax command (jquery's <code>$.ajax()</code>) that will trigger the RSSFeed class's public <code>getFeed($feed_url)</code> function (which in turn utilizes SimplePie to get the data needed.</p> <p>This uses:</p> <ul> <li>jQuery 1.9.1 to handle the AJAX calls and updating the browser</li> <li>Twitter Bootstrap 2.3.0 for layout / normalization / styling</li> <li>SimplePie 1.3.1 to handle the RSS feeds themselves.</li> </ul> <p>When the page loads, I am using the <code>getFeed($feed_url);</code> to correctly get the data (this works):</p> <pre><code>&lt;div class="row"&gt; &lt;div class="span7" id="feedBody"&gt; &lt;?php $RSSFeed-&gt;getFeed("http://rss1.smashingmagazine.com/feed/"); ?&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>This works as expected. Then a feed item may be clicked and that would trigger the following AJAX command (alerts are there for debugging purposes):</p> <pre><code>"use strict"; //Update the feed $("a.feedName").click(function(e) { e.preventDefault(); var feedURL; feedURL = $(this).attr("href"); $.ajax('model/RSSFeed.php', { data: {url: feedURL}, beforeSend: function() { $("#feedBody").html("&lt;div class=\"alert alert-info span7\"&gt;&lt;button type=\"button\" class=\"close\" data-dismiss=\"alert\"&gt;&amp;times;&lt;/button&gt;&lt;strong&gt;Loading Feed&amp;hellip;&lt;/strong&gt;"); }, cache: false, success: function(result) { $("#feedBody").html(result); alert(result); }, error: function(result) { $("#feedBody").hide(); alert("Error!"); } }); }); </code></pre> <p>Then <code>model/RSSFeed.php</code> sees this request and should use the following to trigger getting the new content:</p> <pre><code>if (isset($_GET['url'])) { $RSS = new RSSFeed(); $RSS-&gt;getFeed($_GET['url']); } else if(isset($_POST['url'])) { $RSS = new RSSFeed(); $RSS-&gt;getFeed($_POST['url']); } </code></pre> <p>This calls my RSSFeed class which is as follows:</p> <pre><code>class RSSFeed { //Get the feed at the $feed_url and echo it out to the browser for the ajax request to display public function getFeed($feed_url) { $feed = new SimplePie($feed_url); $feed-&gt;init(); $feed-&gt;handle_content_type(); foreach ($feed-&gt;get_items() as $item) { $output = "&lt;article&gt;" . "&lt;h3&gt;&lt;a href=\"" . $item-&gt;get_permalink() . "\" title=\"" . $item-&gt;get_title() . "\" class=\"articleTitle\"&gt;" . $item-&gt;get_title() . "&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;"; if ($category = $item-&gt;get_category()) { $output .= $category-&gt;get_label() . " "; } $output .= $item-&gt;get_date(); $output .= "&lt;/p&gt;&lt;p&gt;"; $output .= shorten($item-&gt;get_description(), 600) . "&lt;br /&gt;&lt;br /&gt;" . "&lt;a href=\"" . $item-&gt;get_permalink() . "\" title=\"Read More\" class=\"btn btn-info\"&gt;Read More&lt;/a&gt;"; $output .= "&lt;/p&gt;"; echo $output; }//end foreach($feed-&gt;get_items() as $item) }//end getFeed($feed_url) //Begin setting up to allow Google Reader takeout files to be imported into the database. public function importRSSFeeds($xmlFile, $DB) { $xml = simplexml_load_file($xmlFile); foreach($xml as $feed) { foreach($feed-&gt;outline as $thisFeed) { if($thisFeed-&gt;outline['type'] == "rss") { $DB-&gt;addFeedToDatabase($thisFeed['text'], $thisFeed['title'], "folder", "", ""); foreach($thisFeed-&gt;outline as $feeds) { $DB-&gt;addFeedToDatabase($feeds['text'], $feeds['title'], $feeds['type'], $feeds['xmlUrl'], $feeds['htmlUrl']); } echo "&lt;br /&gt;&lt;br /&gt;"; } } } } //end importRSSFeeds($xmlFile) //Get the feeds from the database and display them on the left for the user. public function getFeedList() { $lastType = ""; $DB = new Database(); $result = $DB-&gt;returnFeedList(); foreach($result as $individualFeed) { if($individualFeed['type'] == "folder") { if ($lastType == "rss") { echo "&lt;/ul&gt;&lt;/div&gt;"; } echo "&lt;li&gt;&lt;a href=\"#\" data-toggle=\"collapse\" data-target=\"#" . str_replace(" ", "", $individualFeed['title']) ."\"&gt;&lt;i class=\"icon-folder-close\"&gt;&lt;/i&gt;" . $individualFeed['title'] . "&lt;/a&gt;&lt;/li&gt;"; echo "&lt;div class=\"collapse in\" id=\"" . str_replace(" ", "", $individualFeed['title']) . "\"&gt;"; echo "&lt;ul class=\"nav nav-list\"&gt;"; } else if($individualFeed['type'] == "rss") { echo "&lt;li&gt;&lt;a href=\"" . $individualFeed['xmlUrl'] . "\" class=\"feedName\"&gt;" . $individualFeed['title'] . "&lt;/a&gt;&lt;/li&gt;"; } $lastType = $individualFeed['type']; } $DB-&gt;closeDatabaseConnection(); } }//end class RSSFeed </code></pre> <p>When checking the javascript alerts, it is triggering the <code>success:</code> case but not returning the echo'd data. What piece am I missing to have the SimplePie class return the correct, <code>echo</code>'d data via AJAX to my <code>id="feedBody"</code> div? What am I missing?</p> <p><strong>EDIT:</strong> Firebug says the following regarding the XHR:</p> <pre><code>//Response Headers Status: 200 OK Connection Keep-Alive Content-Length 0 Content-Type text/html Date Thu, 11 Apr 2013 19:54:33 GMT Keep-Alive timeout=5, max=100 Server Apache X-Powered-By PHP/5.4.4 //Request Headers Accept text/html, */*; q=0.01 Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Cookie __utma=111872281.446718044.1365710024.1365710024.1365710024.1; __utmb=111872281.2.10.1365710024; __utmc=111872281; __utmz=111872281.1365710024.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) DNT 1 Host localhost:8888 Referer http://localhost:8888/Charcoal-Bootstrap_RSSReader/ User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0 X-Requested-With XMLHttpRequest </code></pre> <p>Params appear to be passing over correctly (just one example): <code>url http://iphone.keyvisuals.com/feed/</code></p> <p>Full index.php: <a href="http://pastebin.com/UJiFuBvG" rel="nofollow">http://pastebin.com/UJiFuBvG</a></p> <p>Full RSSFeed.php: <a href="http://pastebin.com/CGU2nQHB" rel="nofollow">http://pastebin.com/CGU2nQHB</a></p> <p>Full main.js (handles the AJAX): <a href="http://pastebin.com/1pPQuPUx" rel="nofollow">http://pastebin.com/1pPQuPUx</a></p> <p>Thanks! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload