使用Phonegap应用程序执行ajax请求时出现问题

use*_*978 7 php ajax rss jquery cordova

我正在尝试使用Phonegap和jQuery创建一个简单的RSS阅读器.我正在学习本教程:http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/.

当我在浏览器中试用代码时,我已经设法让这个工作正常.php文件获取feed并输出它就像我期望的那样.但是当我从我编译的Phonegap应用程序中运行相同的文件时,ajax-request只返回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();
    }

    });

};
Run Code Online (Sandbox Code Playgroud)

这是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
?>
Run Code Online (Sandbox Code Playgroud)

use*_*978 15

我终于设法解决了这个问题! 事实证明,如果您想对某个域(无论是您的本地主机还是其他域)发出请求,您需要将您希望从Xcode中的PhoneGap应用程序请求的服务器列入白名单.之前我没有发现这一点的原因是我没有检查ajax响应中的错误.一旦我这样做,我得到了http状态代码401(未授权)和错误消息" Whitelist rejected".

为了解决这个问题,我PhoneGap.plist在我的项目中打开了文件,在键下ExternalHosts我添加了一个值为的新项目:*.localhost.我还将ajax网址更改为:

url: 'http://localhost/rssApp/www/rss-proxy.php?url=http://www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml'

我在iOS模拟器上编译并运行了应用程序,我的localhost服务器响应了一个非常成功的ajax响应!

对于您希望应用程序连接到的每个外部主机,您必须将其添加到列表中ExternalHosts.例如,如果您希望访问http://google.com/maps/api.php上的API,则必须添加*.google.com到列表中.

当你试图找出服务器没有响应的原因时,有点烦人,但我认为这有利于安全性.希望这可以帮助那些正在努力应对来自PhoneGap应用程序的简单ajax请求的人!