Jquery 无法在Android上使用Phonegap获取json数据

Jquery 无法在Android上使用Phonegap获取json数据,jquery,json,cordova,Jquery,Json,Cordova,我正在尝试为Android创建一个连接到网站数据库的应用程序。 我使用的是phonegap,我想使用JSON将数据从数据库返回到应用程序 我在服务器端创建了一个php脚本来执行一些sql请求,并形成一个有效的json文件 我创建了获取数据的脚本,当我在我的浏览器(在我的桌面上)中运行它时,它就工作了,但是现在我尝试在我的手机上发送应用程序,它不再工作了,就像json请求没有做任何事一样 我在config.xml的白名单中添加了该网站 以下是我正在使用的代码: <script>

我正在尝试为Android创建一个连接到网站数据库的应用程序。 我使用的是phonegap,我想使用JSON将数据从数据库返回到应用程序

我在服务器端创建了一个php脚本来执行一些sql请求,并形成一个有效的json文件

我创建了获取数据的脚本,当我在我的浏览器(在我的桌面上)中运行它时,它就工作了,但是现在我尝试在我的手机上发送应用程序,它不再工作了,就像json请求没有做任何事一样

我在config.xml的白名单中添加了该网站

以下是我正在使用的代码:

 <script>
    $(document).bind("mobileinit", function(){
    $.mobile.allowCrossDomainPages = true;
    });
 </script>

 <script type="text/javascript">

   document.addEventListener("deviceready",onDeviceReady,false);
   function onDeviceReady() 
    {       
        var db = window.openDatabase("GalagoPosts", "1.0", "GalagoPosts", 200000);
        db.transaction(populateDB, errorCB, successCB);
    }

    // Populate the database 
    //
    function populateDB(tx) 
    {
        //tx.executeSql('DROP TABLE IF EXISTS GalagoTuto');
        tx.executeSql('CREATE TABLE IF NOT EXISTS GalagoTuto (id unique, title TEXT NOT NULL, artist TEXT NOT NULL, difficulty INTEGER NOT NULL, date TEXT NOT NULL, link TEXT NOT NULL)');

        // Ajax request
        //
        $url = "http://www.mydomain.com/JSON/json_get_data.php";
        $.getJSON($url, function(data) 
                           {
                           alert("Got Data");
                           $.each(data, function(index, element) 
                                           {                                   
                                           $("#latest").append(element.Title+"<br/>");
                                           });
                           displayPages();
                           });  

    }

    // Transaction error callback
    //
    function errorCB(tx, err) 
    {
        alert("Error processing SQL: "+err.code);
    }

    // Transaction success callback
    //
    function successCB() 
    {
        //alert("Succes");
    }

    // Display main page after loading data
    //
    function displayPages() 
    {
        $("body .splashScreen").delay(10).fadeOut(10, function() {
        $("body .mainApp").fadeIn(10);
        $("#nav-bar .current_page_item a").mouseenter();
        });
    }
    </script>
我尝试了回拨和不回拨,但从未收到“Get Data”警报。 我正在使用Phonegap 3.4.0、jQuery 1.11.1和jQuery mobile 1.4.2

我花了大量的时间在谷歌上搜索,试图找出答案,但最终我的手机没有任何效果

有人知道怎么做吗? 谢谢。

试试这个

alert(data); //alert("Got Data"); 
如果发出带有json数据的警报


检查该警报的
标题
它可以正常工作。代码很好。

我终于找到了答案。我真不敢相信这有多愚蠢

我使用此代码包括jquery:

<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>

但如果不在config.xml中添加此项,则它将不起作用:

<access origin="http://code.jquery.com" subdomains="true" />

我认为没有必要允许jquery.com进入白名单


谢谢你的帮助。

我试过了,但没用。奇怪的是,如果alert(“Get data”)不是,为什么alert(data)会工作?该alert()将显示从服务器接收的json内容。如果警报中出现blank,那么服务器没有响应,如果警报没有出现,那么代码中会出现一些错误。您可以在Consel(F12)chrome或firefox中粘贴相同的代码
<access origin="http://code.jquery.com" subdomains="true" />