Josh
Josh

Reputation: 431

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

Upvotes: 8

Views: 4746

Answers (2)

Jonas Äppelgran
Jonas Äppelgran

Reputation: 2747

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.

Upvotes: 2

Drew Dahlman
Drew Dahlman

Reputation: 4972

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/

Upvotes: 8

Related Questions