Webservice in PhoneGap - android

I am a newbie to phonegap. I m stuck. I m unable to get result while calling webservice in phone gap. I used both Ajax and getJSON Method. Getting result as undefined, RealData variable as undefined. I am feeling that I am missing something. Please help guys.
Here is my code....
function onDeviceReady() {
console.log("Device is Ready111111");
alert("alert Device ready");
/*var jqxhr = $
.getJSON(
'http://www.infoline.in/mobdata.aspx?
reqfor=area_list&city=ahmedabad',
function() {
console.log("Device is Ready111111....."+jqxhr);
alert('success');
}).error(function() {
alert('error');
});*/
$(document).ready(function () {
$.ajax({
url: "http://www.infoline.in/mobdata.aspx",
data: {
"reqfor": "area_list",
"city": "ahmedabad"
},
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, textStatus, jqXHR) {
alert(textStatus);
var theRealData = msg.d;
alert(theRealData.length)
}
});
});

In order to view the Object you have to convert the msg into a string.
try this:
console.log(JSON.stringify(msg));

Related

Ajax is not working in android phone using phonegap

"it always alert failure but in case of browser its working fine nor its send data to ajax url "
jQuery.ajax({
url:'http://182.18.164.87/jd/action.php?action=register',
crossDomain: true,
type: 'POST',
data: data,
contentType: "application/json",
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data,status){
alert('success');
localStorage.clear();
return true;
},
error: function(){
alert('failure')
return false;
}
});
try like this :
$.ajax({
type: "POST",
url: "http://182.18.164.87/jd/action.php",
data: {"data":val},
cache: false,
async:false,
success: function(data){
var data = JSON.parse(data);
alert('success');
localStorage.clear();
},
error: function(){
alert('failure')
return false;
}
});
You have to whitelist the URL host (i.e. http://182.18.164.87/) in your config.xml, otherwise it will be blocked for security reason:
<access origin="http://182.18.164.87" />
You will need the whitelist plugin too.

JSON result not getting in AJAX

The JSON didn't give correct result(issuccess:failed) on AJAX while it give correct result in postman(issuccess:true)
The JSON response in AJAX
and
The JSON result in postman
var jData = {};
jData.username = $scope.mobile;
jData.password = $scope.password;
jData.entrytoken="aaad1323dfd";
var $resData = null;
$.ajax({
url:url,
type: "POST",
ContentType: "application/json; charset=utf-8",
data:JSON.stringify(jData) ,
dataType: "json",
beforeSend: function () {
},
success: function (response) {
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown) {
},
complete: function () {
}
});

calling a web service in cordova in Visual studio 2015

I am developing an android app using apache cordova tools in visual studio 2015. I want to call a web service from my index page in cordova app, but I somehow can't achieve it.
Here is the HTML
<div ><input type="button" id="callwebmethod" name="submit" /> <br /> </div>
Here is the JS function
<script type="text/javascript">
$('#callwebmethod').click(function () {
var params = "{'msg':'From Client'}";
$.ajax({
type: "POST",
url: "http://mysite/index.aspx/GetEmployees",
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { alert(result.d); }
});
})
</script>
Here is the web method
[WebMethod]
public static string GetEmployees()
{
return "Hello World";
}
Your var params have to be simular to the Parameters of the WebMethod. Just leave them empty and try it again. They have to be exactly the same.
If you whant to use web methods with parametes here is a working example:
$.ajax({
url: "http://systemservice/systemservice.asmx/App_Test",
data: "{ par1: '" + xxx + "', par2: '" + xxx + "'}",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d) {
//Do something
}
},
error: function (xhr) {
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
})
[WebMethod]
public string App_Test(string par1, string par2) {
return "Hello";
}
With the shown error function you can also find out what is going wrong.
To do it without the paremeters you just have to leave them empty.
data: "{}"
[WebMethod]
public string App_Test() {
return "Hello";
}
It woked for me this example:
var person = {};
person.ID = $('#txtID').val();
person.Name = "Amir";
var pdata = { "p": person };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/SampleService.asmx/GetPesonnelSalary",
data: JSON.stringify(pdata),
dataType: "json",
async: true,
success: function (data, textStatus) {
if (textStatus == "success") {
if (data.hasOwnProperty('d')) {
msg = data.d;
} else {
msg = data;
}
alert(msg);
}
},
error: function (data, status, error) {
alert("error");
}
});
The complete code is here: http://www.scriptiny.com/2012/12/calling-asmx-web-service-via-jquery-ajax-2/

Jquery ignore error and success json code

I trying consume service with phonegap and android.
My service using localhost return the json with chrome:
{
"GetListaMunicipiosResult": [{
"MunicipioID": "1",
"MunicipioNome": "Florianópolis",
"MunicipioUf":"SC"
}, {
"MunicipioID": "2",
"MunicipioNome": "Jaraguá do Sul",
"MunicipioUf": "SC"
}]
}
In my .js file, I call the GET json with the code:
$('#cidades_page').live('pageshow',function(event){
$.ajax("http://10.0.2.2:56976/MunicipiosService.svc/ListaMunicipios",{
beforeSend: function (xhr) {
$.mobile.showPageLoadingMsg();
alert("beforeSend");
},
complete: function () {
// $.mobile.hidePageLoadingMsg();
alert("complete");
},
contentType: "application/json",
dataType: "jsonp",
jsonp: "callback",
type: "GET",
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
//alert(thrownError);
},
success: function (data) {
alert(JSON.stringify(data));
}
});
});
But when page show, only alert alert("beforeSend") fired, and after nothing happens.
I insert the json call in html using $.ajax(.... and open with chrome and its work. I do not know what else to do.
thanks for help
EDIT
I test in windows phone and now i can get error Error:GetListaMunicipios was not called.
My .js:
$.ajax("http://localhost:56976/MunicipiosService.svc/ListaMunicipios?callback=?",{
beforeSend: function (xhr) {
// $.mobile.showPageLoadingMsg();
alert('beforeSend');
},
complete: function () {
// $.mobile.hidePageLoadingMsg();
alert('complete');
},
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
crossDomain: true,
jsonp: 'callback',
jsonpCallback:'GetListaMunicipios',
type: 'GET',
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
},
success: function (data) {
alert('success');
}
});
My WCF Service
namespace GuiaService
{
[ServiceContract]
public interface IMunicipiosService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "ListaMunicipios")]
List<ClsListaMunicipios> GetListaMunicipios();
}
}
thanks for help.
You should use done, fail and always.
success, error and complete are deprecated.
Update: as noted in the comments, this isn't true for how you're using it. I now believe the problem is because you're using jsonp as the type instead of json.
jsonp is designed to call a function automatically when loaded, so your server should be adding a function call to the generated code. It's possible that jQuery expects this behaviour and disables its own callbacks, or uses the jsonp mechanism to trigger its own callbacks, but since your server doesn't actually add the function call, nothing happens.

How to call WCF webservice using PhoneGap which returns SOAP object?

Can anyone tell me,
How to call WCF webservice using PhoneGap/HTML5/Jquery which returns SOAP object?
I want to call WCF service from phonegap application.
try this. may be this will help you.becouse i have done webservices u using this
<script type="text/javascript">
$(function () {
var url = "http://localhost/newed/WebService.asmx/GetList?format=json";
$.ajax({
contentType: "application/json; charset=utf-8",
url: url,
dataType: "jsonp",
success: function (r) {
var i = 0;
for (i = 0; i < r.d.length; i++) {
alert(r.d[i]["firstName"].toString());
}
},
error: function (xr, msg, e) { debugger; alert(e); }
});
});

Categories

Resources