Phonegap Android app web service - ajax

I am making Phone-gap app with android SDK using eclipse. In which i called web service for inserting data into database through AJAX call But i think url is not working properly. I am using localhost web service in running with emulator. My ajax call code is as per below.
$.ajax({
type: "POST",
contentType: "application/json",
dataType: "json",
async: false,
cache: false,
url:"http://xxx.xxx.x.xxx:8080/WebService2.asmx/HelloWorld",
data:"{'n':'" + document.getElementById('txtNum1').value + "', 'a':'" + document.getElementById('txtNum2').value + "','p':'" + document.getElementById('txtNum3').value + "'}",
success: function (response)
{
alert("Inserted data successfully");
document.getElementById('txtNum1').value="";
document.getElementById('txtNum2').value="";
document.getElementById('txtNum3').value="";
},
error: function(data) {
alert("Try Again");
}
});
}

If you want to access localhost from emulator then you need to use the IP address 10.0.2.2.
Try the url http://10.0.2.2:8080/WebService2.asmx/HelloWorld for your web-service when accessing it from emulator.
You can read more about Android emulator networking from the resource:-
http://developer.android.com/tools/devices/emulator.html#networkaddresses

Related

How to invoke a web service published on a service bus using ajax?

I'm working with Oracle service bus, I've currently created services with SOA in jdeveloper which I have published on the service bus that offers oracle, now I need to access them from my mobile application made in phonegap. Does anyone know how to do it?. I have used a function in ajax but it does not work:
conectar = function () {
var url = "http://10.0.0.191:7001/OSBCapled/Proxy_Services/AutenticacionWS?WSDL"
var datos = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '</soap:Header>'
+ '<soapenv:Body>'
+ '<aut:requestRegistro xmlns:aut="http://www.integracion.org/autenticacion">'
+ '<aut:Email>'+$('#usuario').val()+'</aut:Email>'
+ '<aut:Clave>'+$('#clave').val()+'</aut:Clave>'
+ '</aut:requestRegistro>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
$.ajax({
url: url,
type: "POST",
crossDomain: true,
async: false,
data: datos,
cache:false,
dataType: 'text/xml',
success: function (data) {
alert("Funciono!! " + data.responseText);
},
error: function (data) {
alert("Error"):
},
});
}
Your URL has ?wsdl at the end. This implies that you want the specification of the service, rather than sending messages to the service.
Remove that, and you might have more luck.

Not able to make ajax request in windows phone phonegap

I have developed android app using phonegap. Now I am trying to use that code for developing windows phone 8 app. I have successfully set up the Visual Studio Express environment and made Cordova Template. After that I have created the project and inserted my assets into that project.
It shown me my app UI perfectly and events, page navigation are working fine. But it is not able to make ajax http connection. Do I need enable any thing in SDK or Project Directory.
When am I trying this , I am getting alert that 'Connectivity Failure'
This is my sample js code for making http connection
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg("a","Processing !!! Please wait "); },
complete: function() { $.mobile.hidePageLoadingMsg(); },
type: 'POST',
url:'http://192.168.12.175/Receiver.php',
data: reqObj ,
success: function(data){
responseString=data;
}
error: function(data){
//get the status code
alert('Connectivity Failure');
} });
use this one
$.mobile.showPageLoadingMsg("a","Processing !!! Please wait ");
formData = {
param1: param1
}
$.ajax({
type: 'POST',
contentType: 'application/json',
url: "http://192.168.12.175/Receiver.php",
dataType: "json",
data: formData,
success: function(data) {
$.mobile.hidePageLoadingMsg();
},
error: function(data) {
alert('Connectivity Failure');
}
});
Adding
<feature name="http://api.phonegap.com/1.0/network"/>
in config.xml fixed the issue for me.
This is adding the <Capability Name="ID_CAP_NETWORKING" /> into WMAppManifest.xml file when build is generated

phonegap 3.1.0-0.15.0 remote ajax call

I am developing an app in phonegap 3.1.0-0.15.0 and I am having problems with an ajax call to a remote server.
I have done all the default stuff, like added Internet acces, with-listed the domain, but the ajax still get's the 404 header.
My ajax call:
$.ajax({
url: 'http://my-host/bus/app-ajax',
type: 'GET',
dataType: "json",
data: 'type=data_out',
cache: false,
crossDomain: true,
processData: false,
contentType: false,
success: function(ret_data) {
alert(ret_data);
},
error: function(xhr, textStatus, errorThrown) {
alert("Ajax error(ajax_helper.get_categorys) xhr.status: " + xhr.status);
}
});
My code has an IP address in the "url" variable of the ajax call, I just changed it here for security reasons.
If I copy the content of my www folder to a website that is on the same host as my PHP respond script(browsers don't allow cross domain ajax calls) than it works. On the device that I am testing if I open a browser I can access the domain, and get the json response, but the app just gives me 404 all the time.
Any ideas on what I can still try to make this work?
I recreated the project with the verbose "phonegap create" command, added the same code, and it works. Was probably some problems with the old project, because I updated phonegap in it a few times.

phonegap api call not working

I have a rails api running on localhost:3000. On my phonegap app i'm trying to login via api , but it give an error("error") and don't reach to the api.
could be some problem with cross domain request, but is said that in the mobile there is no such thing. internet is working .
my ajax:
$.ajax({
url: 'localhost:3000/api/v1/login',
type: "POST",
dataType: "json",
data : {username:"demo",password:"demo"},
success: function(data) {
navigator.notification.alert("ok");
},
error: function(xhr, textStatus, errorThrown) {
console.log("Error status " + status);
}
});
EDIT 2 - solved
You have to to use local ip eg. 192.168.2.2
and add the host in the Cordova.plist in the ExternalHosts key.
It is because you are using "localhost" when you run this code on a device/emulator then "localhost" refers to the device/emulator itself and not the web server that contains your rails API. You'll need to switch "localhost" to the IP address or hostname of your server.

Jquery Webservice in ASP.NET

I am working with ASP.NET 4.0 framework. In one web page, I have to call web service using Jquery as
var serviceurl = 'http://www.websitename.com/webservicename';
$.ajax({
type: "POST",
url: serviceurl + 'WebServiceName',
data: "{'Parameters': '" + parameter+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
ShowAfterSuccess(msg);
},
error: AjaxFailed
});
It works fine, if i mention url as "http://www.websiteName.com" but when i put URL as "websitename.com" it doent call webservice.
but it works well only in Google Chrome with "websiteName.com" I dont knw what is the issue with that....whether there is problem in my webservice calling or in URL..
You must ensure that you are not violating the same origin policy restriction. The best way to ensure this is to use relative urls:
var serviceurl = '/webservicename';
You must ensure that the domain hosting this javascript matches exactly the domain that you are sending your AJAX call to (including the protocol).

Resources