2011-10-24 29 views
2

基本上我有一個位於服務器上的php腳本,它生成一個JSON文件,列出來自mysql數據庫的地方。使用jQuery Mobile我正在開發一個應用程序來顯示這些地方。我的代碼在Chrome & Safari中工作,但是當我將它移植到Phonegap時,它不起作用。我已搜查所有在互聯網上,但無法找到答案:(在Phonegap中加載JSON?

php文件生成JSON(json.php):

<?php 
header('Content-type: application/json'); 


$server = "localhost"; 
$username = "xxx"; 
$password = "xxx"; 
$database = "xxx"; 

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error()); 
mysql_select_db($database, $con); 

$sql = "SELECT * FROM places ORDER BY name ASC"; 
$result = mysql_query($sql) or die ("Query error: " . mysql_error()); 

$records = array(); 

while($row = mysql_fetch_assoc($result)) { 
    $records[] = $row; 
} 

mysql_close($con); 

echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');'; 
?> 
位於我的應用程序內

我的JavaScript文件(負載JSON並將其顯示):

$('#places').bind('pageinit', function(event) { 
    getPlaces(); 
}); 


function getPlaces() { 

    var output = $('#placeList'); 

    $.ajax({ 
     url: 'http://www.mysite.com/json.php', 
     dataType: 'jsonp', 
     jsonp: 'jsoncallback', 
     timeout: 5000, 
     success: function(data, status){ 
      $.each(data, function(i,item){ 
       var place = '<li><a href="">'+item.name+'<span class="ui-li-count">' 
       + item.checkins+'</span></a></li>'; 

       output.append(place); 
      }); 
      $('#placeList').listview('refresh'); 
     }, 
     error: function(){ 
      output.text('There was an error loading the data.'); 
     } 
    }); 

} 

的HTML看起來像這樣:

<div data-role="content"> 
      <h3>Places</h3> 
      <ul data-role="listview" id="placeList" data-inset="true"> 

     </ul> 
</div><!-- /content --> 

此代碼在Chrome & Safari,但是在帶有Phonegap的xCode模擬器中運行時,它不會加載JSON。

任何幫助,將不勝感激:)

+0

我有一個類似的問題。我也從服務器返回json。我可以手動輸入網址,它適用於我的桌面上的任何瀏覽器,但如果我在我的Android設備上的移動瀏覽器中嘗試它,我會收到404錯誤。 – blong824

回答

0

這有可能是一些做的白名單,蘋果公司在任何類型的不可控的數據沒有信心(如I幀或飼料)。所以他們拒絕每一個外部連接。

看看你的項目rootfolder(xcode)中的phonegap.plist,並將你的網站url添加到數組'ExternalHosts'中。這可能會工作後罰款。