Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to communicate with a server using PhoneGap?

I'm wondering if anyone has any advice regarding using PhoneGap to send and receive information from a web server. Is there a standard way of doing this? Any best practices? I'm pretty new to app development and any advice would be helpful.

Thanks

like image 456
Josh Avatar asked Jan 28 '12 20:01

Josh


2 Answers

I personally use jQuery ajax. The awesome thing about phonegap and running js on a phone is that you have no normal javascript security issues like crossdomain issues.

One thing you need to remember is that in order to reach outside servers you will need to add a new key to your plist in your external hosts KEY: websites VALUE: *

the * is a catch all so any domain can be accessed.

as for the ajax treat it like a normal ajax request:

$.ajax({
  url:'http://your-url.com/script.php',
  type:'post',
  data:'arg=foo&argB=bar',
  success:function(data){
    console.log(data);
  },
  error:function(w,t,f){
    console.log(w+' '+t+' '+f);
  }
});

good luck happy deving!

I've got a few phonegap tutorials on my blog - http://www.drewdahlman.com/meusLabs/

like image 93
Drew Dahlman Avatar answered Oct 07 '22 11:10

Drew Dahlman


Use any AJAX you want.

Remember to allow the server you're going to communicate in your config.xml file!

<access /> - deny all
<access origin="*" /> - allow any
<access origin="http://example.com*" subdomains="true" /> - allow all of example.com

There are more examples in the config.xml file.

like image 37
Jonas Äppelgran Avatar answered Oct 07 '22 12:10

Jonas Äppelgran