0

I am making a simple mobile app with Cordova. The code I have works perfectly when I test it in a browser but it ignores the success function when test on an Android phone and prints directly the error message from db.js.

Here is the relevant index.html

 <body>
    <div class="app">
        <h1></h1>
        <div id="deviceready" class="blink">
            <div>
                <div id="output"></div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/db.js"></script>
</body>

db.js with the ajax function

$(document).ready(function() {
  $(document).bind('ready', function() {
   var output = $('#output');

   $.ajax({
    url: 'http://domainName/server/folder/db.php',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data, status){
       $.each(data, function(i, item){
       var cards = '<h1>' + item.title + '</h1>' + item.body 
       + '<br>' + '<img src="img/' + item.slug + '.jpg"' + '>' + '<br>' ;

       output.append(cards);

    });
  },
  error: function(){
      output.text("There was an error loading the data.");  
  }
 });
 });
 });

db.php

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

   $host = 'hostname';
   $dbname = 'database';
   $user = 'username';
   $pass = 'passoword';

   $jsoncallback = isset($_GET['jsoncallback'])? $_GET['jsoncallback'] : "";

   $con = mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error());
   mysql_select_db($dbname, $con);

   $sql = "SELECT * FROM cards";
   $result = mysql_query($sql) or die("Query error: " . mysql_error());

   $records = array();

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

   mysql_close($con);
        echo $jsoncallback . '(' . json_encode($records) . ');';

All domains are whitelisted:

<access origin="*" />

Permissions in Android manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Any ideas?

liharts
  • 1
  • 1
  • 2
  • Have you tried to print the error message: `function(e){ output.text("Error: "+e); }`? – lifeisfoo Sep 02 '15 at 12:15
  • Yes: `error:function(){output.text("There was an error loading the data.")};` Prints it on the device screen. While the success function executes on the browser. – liharts Sep 02 '15 at 12:30
  • In cordova you should wait for the `deviceready` event before any actions. See this: http://stackoverflow.com/a/32351495/3340702 – lifeisfoo Sep 02 '15 at 12:36

2 Answers2

0

Have you tried putting your code inside deviceready? Suppose your html starting index file look likes below,

<html>
<head> </head>
<body onload="onBodyLoad()" >
</body>
</html>

and your script file look likes below.

<script>
function onBodyLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
// do your thing here!
}
</script>
locknies
  • 1,345
  • 3
  • 15
  • 36
  • Deviceready runs at the start of the app like [this](https://github.com/apache/cordova-app-hello-world/blob/master/www/js/index.js). I did put the jquery code under bindEvents (in the index.js) function just in case but I got the same results: script executes successfully on the browser, unsuccessfully on the device. – liharts Sep 02 '15 at 17:40
0

I made it work by building a new project with Phonegap (instead of Cordova). Exactly the same code otherwise. Still no idea why it wouldn't work with Cordova.

liharts
  • 1
  • 1
  • 2