3

I have created a simple PhoneGap application using PhoneGap Build. I have a MySQL DB and write a simple PHP file to read some data from it and give it as JSON format. I try to read that info in an HTML file in a PhoneGap application but my mobile does not show anything. Anyone can help me to find the problem? I also add cordova-plugin-inappbrowser plugin in my app. here is the code example I used:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>READ JSON Example (AJAX)</title>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
   $.ajax({
     type: "GET",
     url: "http://phonegappro.esy.es/test/json.php",
     crossDomain: true,
     cache: false,
     success: function(result){
     var result=$.parseJSON(result);
     $.each(result, function(i, field){
       $("#output").append("Title: "+ field.title + " duration: "+field.duration  +" Price:"+field.price+"<br/>");
     });
    }
  });
});
</script>
</head>
<body>
  <div id="output"></div>
</body>  
Skam
  • 7,298
  • 4
  • 22
  • 31
Abbas
  • 41
  • 8
  • 2
    All ok with your backend? Headers are sending "`Content-Type:application/json`" and such? – yuriy636 Aug 08 '16 at 23:22
  • 1
    Why not use `$.getJSON()`? – StackSlave Aug 08 '16 at 23:33
  • 1
    @PHPglue It doesn't work with cross-domain AJAX calls. See [this question](http://stackoverflow.com/q/6849802/5743988). – 4castle Aug 08 '16 at 23:34
  • 1
    Does `#output` have a height and width? This code worked on my localhost. – StackSlave Aug 08 '16 at 23:47
  • 1
    `@4castle` `$.getJSON()` worked for me. – StackSlave Aug 08 '16 at 23:52
  • is phonegappro.esy.es whitelisted for your app? This code works for me as well – Zachary Wand Aug 09 '16 at 02:15
  • Try `alert(result);` in success callback. – Hardik Vaghani Aug 09 '16 at 04:14
  • UPDATE: I checked the same code and application on iOS. On iPhone it works. It seems that the problem is on android devices. I checked it on 3 different android devices (Samsung galexy s4, Nexus 4 and HTC desire C) . So The code and backend is fine and working correct. Anyone knows what is the possible problem on the Android ? – Abbas Aug 09 '16 at 05:07
  • @PHPglue: I also use `$.getJSON()` but it is not working on the android devices same as previous code. I think there should be something in the config.xml file. By the way, I have these lines in the config.xml also: `` `` `` – Abbas Aug 10 '16 at 00:47
  • FINAL UPDATE: I found the answer: I added a PhoneGap Build whitelist plugin and then it work correctly on android devices also. This is the plugin: `` – Abbas Aug 10 '16 at 01:13

1 Answers1

0

I found the answer: I added a PhoneGap Build whitelist plugin and then it work correctly on android devices also. This is the plugin: <plugin name="com.indigoway.cordova.whitelist.whitelistplugin" spec="1.1.1" source="pgb" />

Moreover, in my config.xml file, I have: <access origin="*"/> too

Abbas
  • 41
  • 8