首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Phonegap应用程序执行ajax请求时遇到的问题

使用Phonegap应用程序执行ajax请求时遇到的问题
EN

Stack Overflow用户
提问于 2011-11-05 19:45:40
回答 2查看 7.1K关注 0票数 7

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

当我在浏览器中尝试代码时,已经成功地实现了这个功能,-php文件获取提要并输出它,就像我期望的那样。,但是当我在编译的Phonegap应用程序中运行同一个文件时,ajax-request只返回php-文件的内容(php-代码,而不是执行的结果)。

我花了几个小时在谷歌上搜索,并尝试了许多教程和微调。我在官方的Phonegap论坛上也没有找到解决办法。我做错了什么?问题似乎是PHP没有响应请求。我尝试将php文件移动到不同的域,但结果是一样的,它在我的浏览器中工作,但在编译的应用程序中不起作用。

下面是启动ajax代码的jQuery代码:

代码语言:javascript
复制
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并输出它:

代码语言:javascript
复制
<?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
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-07 18:02:28

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

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

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

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

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

当您试图找出服务器没有响应的原因时,会有些烦人,但出于安全原因,我想这很好。希望这能帮助其他人处理来自PhoneGap应用程序的简单ajax请求!

票数 15
EN

Stack Overflow用户

发布于 2011-11-05 23:27:45

看起来您正在本地运行服务器(基于192.168.x.xIP地址),这意味着只有连接到网络的设备才能访问它。您可以将电话连接到与计算机相同的wifi网络作为临时修复。但是你需要把它放在一个真正的服务器上才能在互联网上访问。

您还可以将路由器上的端口80转发到此IP地址,然后在请求url中使用实际的IP地址(请参阅whatsmyip.org)。但这不是一个真正稳定的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8022800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档