我正在尝试使用Phonegap和jQuery创建一个简单的RSS阅读器。 我正在关注本教程:http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/。使用Phonegap应用程序执行ajax请求的问题
当我在浏览器中试用代码时,我已经成功实现了这项工作。 php文件提取feed并将其输出,就像我期望的那样。 但是,当我从编译的Phonegap应用程序中运行相同的文件时,ajax-request只返回php文件(php代码,而不是执行结果)的内容。
我花了几个小时搜索这个,并尝试了大量的教程和调整。我在官方的Phonegap论坛上也找不到解决方案。我究竟做错了什么?这个问题似乎是PHP没有回应请求。我试图将php文件移到不同的域,但结果是一样的,它在我的浏览器中工作,但不在编译的应用程序中。
这里是jQuery代码初始化Ajax代码:
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
?>
我建议从代码中删除该IP地址。 – sciritai
当您在模拟器或设备上的浏览器中打开'.php'文件时会发生什么? PHP是否被执行? – Marko
感谢您的评论!我试图从模拟器中的浏览器访问'.php'文件,它可以工作。但它只适用于我将网址更改为相对而非绝对:'url:'rss-proxy.php?url = http://www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml' '。如果我现在使用移动Safari浏览器访问我的Phonegap应用程序的www文件夹中的index.html文件,该文件夹位于我的“htdocs”目录中的本地MAMP服务器上,它就可以工作!但不是从编译的Phonegap应用程序。当然,'.php'文件与其他脚本文件一起位于www文件夹中。 – user1029978