[英]Phonegap cannot display php content retrieved via jQuery
我有一個簡單的PHP,如下所示,還有一個HTML文件(由phonegap-index.html頁提供的文件)和一些jQuery代碼,用於從托管在localhost上的PHP獲取內容。 一切在瀏覽器中都可以正常工作,但是在phonegap中,我只能看到html代碼,即看不到通過jQuery從PHP接收到的內容。 我已將我的IP地址和localhost添加到phonegap的config.xml文件中以進行白名單。 jQuery回調設置為JSONP。 Phonegap服務器安裝在ubuntu上,運行良好,iPhone 5s上安裝了phonegap應用程序/客戶端,該應用程序/客戶端顯示任何簡單的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>
當您連接到本地主機時,它可以在瀏覽器中運行,但不能在手機上運行。 localhost
或127.0.0.1
只是一個主機名,可用於訪問在該主機上運行的網絡服務。
您的php服務器未在iPhone上運行,因此嘗試從電話連接到localhost
沒有任何意義。 您需要連接到服務器的外部可用地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.