我正在尝试使用 Phonegap 和 jQuery 创建一个简单的 RSS 阅读器。
我正在关注这个教程:http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/ http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/.
当我在浏览器中尝试代码时,我已经成功地使其工作正常。php 文件获取提要并输出它,就像我期望的那样。但是当我从编译的 Phonegap 应用程序中运行相同的文件时,ajax 请求仅返回 php 文件的内容(php代码,不是执行结果)。
我花了几个小时在谷歌上搜索这个并尝试了许多教程和调整。我在Phonegap官方论坛也没有找到解决方案。我究竟做错了什么?问题似乎是 PHP 没有响应请求。我尝试将 php 文件移动到不同的域,但结果是相同的,它在我的浏览器中有效,但在编译的应用程序中无效。
下面是启动 ajax 代码的 jQuery 代码:
function get_rss_feed() {
//clear the content in the div for the next feed.
$("#feed_content").empty().html('<img class="loader" src="js/images/ajax-loader.gif" alt=""/>');
$.ajax({
url: 'http://192.168.1.7/rssApp/www/rss-proxy.php?url=http://www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml',
success: function parseRSS(d) {
//find each 'item' in the file and parse it
$(d).find('item').each(function() {
//name the current found item this for this particular loop run
var $item = $(this);
// grab the post title
var title = $item.find('title').text();
// grab the post's URL
var link = $item.find('link').text();
// next, the description
var description = $item.find('description').text();
//don't forget the pubdate
var pubDate = $item.find('pubDate').text();
// now create a var 'html' to store the markup we're using to output the feed to the browser window
var html = "<div class=\"entry\"><h2 class=\"postTitle\">" + title + "<\/h2>";
html += "<em class=\"date\">" + pubDate + "</em>";
html += "<p class=\"description\">" + description + "</p>";
html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";
//put that feed content on the screen!
$('#feed_content').append($(html));
});
$('#feed_content img.loader').fadeOut();
}
});
};
这是rss-proxy.php从 url 加载 XML 并输出它:
<?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
header("Content-Type: text/xml"); // Set the content type appropriately
echo $xml; // Spit out the xml
curl_close($session); // And close the session
?>