加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用Phonegap应用程序执行ajax请求的问题

发布时间:2020-12-13 18:22:59 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用Phonegap和jQuery创建一个简单的RSS阅读器. 我正在学习本教程: http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/. 当我在浏览器中试用代码时,我设法让这个工作得很好. php文件获取feed并输出它就像我期望的
我正在尝试使用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();
    }

    });

};

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

为了解决这个问题,我在我的项目中打开了文件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请求的人!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读