Calling web services using ajax, jquery in phoneGap in android


Hey folks...

Today I am going to post one useful code for those who face some problem in calling web services in android using phonegap.

This is a very simple example for calling  a web service through ajax..

So here we go.

 $.ajax({
url:"url",
type:"GET",
data:'UserId=1',
dataType:"json",
error:function(jqXHR,text_status,strError){
alert("no connection");},
success:function(data){
var i = data;
var data_string = JSON.stringify(i);
                alert(data_string);
                }
        });
If there is an error in calling aja erquest then it will go to error function else in success function.
so friends if you want to send parameters with the url the url pattern and data will look like this

var url="http://someIPAddress/bla/bla/serviceName"
and send parameter like this.

 var para="email="+mail+"&password="+pwd;

Then our ajax request look like this:-

 $.ajax({
url:"url",
type:"GET",
data:para,
dataType:"json",
error:function(jqXHR,text_status,strError){
alert("No connection");},
success:function(data){
var i = data;
var data_string = JSON.stringify(i);
                alert(data_string);
                }
        });


points to remember:-

1- parameter Name should be same as in your web service parameter, in my case email & password.
2- Type should be same as in your web service.
3- dataType should be same as in your web service. 
Enjoy.

Happy coding :-)

~~ Suraj Shukla