在Phonegap中加载JSON? [英] Loading JSON in Phonegap?

查看:120
本文介绍了在Phonegap中加载JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个php脚本位于服务器上生成一个JSON文件列出的地方从一个mysql数据库。使用jQuery Mobile我正在开发一个应用程序来显示这些地方。我的代码适用于Chrome& Safari,但是当我将它移植到Phonegap它不工作。我已经在互联网上搜索,但找不到答案:(。

Basically I have a php script located on a sever that generates a JSON file listing places from a mysql database. Using jQuery Mobile I am developing an application to display these places. My code works in Chrome & Safari, however when I port it over to Phonegap it doesn't work. I have searched all over the internet but can't find an answer :(.

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

The php file for generating 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并显示它):

My Javascript file located within my app (Loads JSON and displays it):

$('#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,但是在xCode模拟器中使用Phonegap运行时,它不会加载JSON。

This code works in Chrome & Safari, however when run in the xCode simulator with Phonegap it doesn't load the JSON.

任何帮助将非常感谢:)

Any help would be much appreciated :)

推荐答案

这可能与白名单有关,苹果不信任任何类型的不可控制的数据(如iframe或feed)。

This has probably something to do with the whitelisting, Apple has no faith in any type of uncontrollable data (like iframes or feeds). So they reject every external connection.

在您的项目根文件夹(xcode)中查看phonegap.plist,并将您的网站网址添加到数组ExternalHosts。这可能会工作后。

Take a look at phonegap.plist in your project rootfolder (xcode) and add your website url to the array 'ExternalHosts'. This will probably work fine after.

这篇关于在Phonegap中加载JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆