2015-10-27 49 views
1

我有一個簡單的PHP,下面有一個HTML文件(這是phonegap-index.html頁面提供的文件),它包含一些jQuery代碼以從PHP獲取內容,在本地主機上。在瀏覽器中一切正常,但在phonegap中,我只能看到html代碼,也就是說,我沒有看到通過jQuery接收到的內容。我已將我的IP地址和本地主機添加到phonegap的config.xml文件中用於列入白名單。 jQuery回調被設置爲JSONP。 Phonegap服務器安裝在運行良好的ubuntu上,iPhone 5s上安裝了PhoneGap應用程序/客戶端,它顯示了任何簡單的html內容,但沒有通過jQuery接收到php內容。Phonegap無法顯示通過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> 

回答

1

它可以在瀏覽器中運行,但不會在手機上運行,​​因爲您正在連接到本地主機。 localhost127.0.0.1只是一個可用於訪問該主機上運行的網絡服務的主機名。

你的php服務器沒有運行在你的iPhone上,所以試圖從手機連接到localhost沒有任何意義。您需要連接到服務器的外部可用地址。

+0

感謝您的回答山姆。我可以從iPhone瀏覽器訪問我的機器上的本地主機,但如果我使用IP地址192.168.0.9,則可以從我的iPhone訪問gettext.php。這個問題對我來說沒有意義,因爲我的iPhone瀏覽器中顯示的是我的PHP文件的HTML內容顯示在我的iPhone瀏覽器中,但是通過jQuery檢索到的PHP消息被忽略了?如果我可以從iPhone上看到託管在我的本地主機服務器上的HTML內容,那麼爲什麼不顯示PHP消息。 – noviceAppDev