Phonegap不能显示通过jQuery检索到的php内容


Phonegap cannot display php content retrieved via jQuery

如下所示,我有一个简单的PHP和一个HTML文件(由phonegap-index.html页面提供的文件),其中包含一些jQuery代码,用于从托管在本地主机上的PHP获取内容。在浏览器中一切工作正常,但在phonegap中,我只能看到html代码,也就是说,我看不到通过jQuery从PHP接收的内容。我已经将我的ip地址和本地主机添加到phonegap的config.xml文件中,以便加入白名单。jQuery回调设置为JSONP。Phonegap服务器安装在ubuntu上运行良好,Phonegap应用程序/客户端安装在iPhone 5s上,显示任何简单的html内容,但不显示通过jQuery接收的php内容。

我相信问题出在phonegap上,但是我已经用了三天了,还是找不到我做错了什么。

以下是我的文件:index . html

   <html>
   <head>
   <meta charset="utf-8" />
   <link rel="stylesheet" type="text/css" href="css/index.css" />
   <script type="text/javascript" src="cordova.js"></script>
   <script type="text/javascript" src="js/index.js"></script>
   <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
    <!-- jQuery to get data from gettext.php page -->
   <script>
   $(document).ready(function(){
      $.getJSON('http://localhost/hello/gettext.php', function(jsonp){
      $("#txtHint").html(JSON.stringify(jsonp, null, 2));
      });
   });
   </script>
   </head>
   <body>
   <br /><br /><br />
   <div class="app">
       <h1>PhoneGap</h1>
       <div id="deviceready" class="blink">
           <p class="event listening">Connecting to Device</p>
           <p class="event received">Device is Ready</p>
       </div>
   </div>
   <script type="text/javascript">
      app.initialize();
   </script>
   <!-- this section is populated by jQuery -->
   <p>php content received via jQuery: <span id="txtHint"></span></p>
   </body>
   </html>

这是我的gettext.php页面

  <?php
   header('content-type: application/jsonp; charset=utf-8');
   header("Access-Control-Allow-Origin: *");
   echo json_encode("Hello World!");
   ?>

下面是config.xml文件中用于添加白名单的特定部分

<access origin="*" />
   <access origin="http://127.0.0.0/hello2/gettext.php" />
   <access origin="http://localhost/hello2/gettext.php" />
   <plugin name="cordova-plugin-whitelist" version="1" />
   <allow-intent href="http://localhost/hello2/gettext.php" />
   <allow-intent href="http://*/*" />
   <allow-intent href="https://*/*" />
   <allow-intent href="tel:*" />
   <allow-intent href="sms:*" />
   <allow-intent href="mailto:*" />
   <allow-intent href="geo:*" />
   <platform name="android">
     <allow-intent href="market:*" />
   </platform>
   <platform name="ios">
     <allow-intent href="itms:*" />
     <allow-intent href="itms-apps:*" />
   </platform>

它可以在浏览器中工作,但在连接到本地主机时不能在手机上工作。localhost127.0.0.1只是一个主机名,可用于访问运行在该主机上的网络服务。

你的php服务器没有在你的iPhone上运行,所以试图从手机连接到localhost是没有意义的。您需要连接到服务器的外部可用地址。