call local .asmx webservice from phonegap android app to get data? - android

i m creating one phonegap test application, for that i want data from database through webservice (.asmx). i m just newer for this technology. i have spent 2 days for this call posible bt couldnt.
function test()
{
jQuery.support.cors = true;
$.mobile.allowCrossDomainPages = true;
$.ajax({
data: "{}",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://localhost:xxxxx/yyyy/testservice.asmx?op=testfunction",
success: function (msg) {
$('#divToBeWorkedOn').html(msg.text);
},
error: function (e) {
$('#divToBeWorkedOn').html("unavailable");
}
});
}
Its local service.

I would replace http://localhost:xxxxx/ with the actual internal IP address of the server (probably something like http://192.168.x.x:xxxxx/) - just make sure your server and mobile device are connected to the same network through wi-fi not gsm network.
Also make sure you updated the domain white list. more info how to do that can be found here http://docs.phonegap.com/en/2.2.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide
hope this will help, good luck!

Related

Android WebView AJAX Request

I'am develoing a VB.NET MVC4 Web App using Android's webView to run it.
I have the next problem: When i make an ajax PUT request to my own controllers method, i recieve a 404 status code.
In the other hand, when i make a get or post petition, works fine. ¿Any idea?
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
jqXHR.readystate = 4
jqXHR.message = server error file or directory not found
jqXHR,status: 404
EDIT: IIS is configured to work with PUT and DELETE and thanks to that it works correctly in the browser but not with the Androids WebView.
Your code is missing a quote url: 'myurl needs a ' on the end
$.ajax({
type: 'PUT',
url: 'myurl',
data: data,
success: function (json, textStatus) {
alert(json);
},
error: function (jqXHR) {
alert('error');
}
});
This might be an error in your example but that's all we can see to correct the other things to check is IIS Setup of the site does is it set to allow PUT, DELETE ect
GET & POST are standard and are enabled by default.
So please follow the answer to this question:
How do I enable HTTP PUT and DELETE for ASP.NET MVC in IIS?

How to load data from another website using jquery mobile application for android ?

I want to build an app using jquery mobile and phonegap. My problem is When I try to load json data using $.ajax() method it works fine in local host but when i try this at live hosting it results nothing. Here is my code.
$.ajax({
type: "GET",
url: "http://rahultest.herobo.com/rbbank/login.php",
crossDomain: true,
data: {id: 'rahul123', pswd:'xxxxx'},
dataType:"json",
success:function(data){
if(data.status=="connected"){
user_id=data.id;
$.mobile.navigate("#menu");
}
else{
alert("User ID and Password is incorrect");
}
},
error: function(){alert("Could not connect");}
});
});
Have you added the domain to Phonegaps whitelist?

Trouble getting $.ajax() to work in PhoneGap against a locally hosted server

Currently trying to make an ajax post request to an IIS Express hosted MVC 4 Web API end point from an android VM (Bluestacks) on my machine. Here are the snippets of code that I am trying, and cannot get to work:
$.ajax({
type: "POST",
url: "http://10.0.2.2:28434/api/devices",
data: {'EncryptedPassword':'1234','UserName':'test','DeviceToken':'d234'}
}).always(function( data, textStatus, jqXHR ) {
alert( textStatus );
});
Whenever I run this request I always get back a textStatus of 'error'. After hours of trying different things, I pushed my End Point to an actual server, and was able to actually get responses back in PhoneGap if I built up an XMLHttpRequest by hand, like so:
var request = new XMLHttpRequest();
request.open("POST", "http://172.16.100.42/MobileRewards/api/devices", true);
request.onreadystatechange = function(){//Call a function when the state changes.
console.log("state = " + request.readyState);
console.log("status = " + request.status);
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
console.log("*" + request.responseText + "*");
}
}
}
request.send("{EncryptedPassword:1234,UserName:test,DeviceToken:d234}");
Unfortunately, if I try to use $.ajax() against the same end point in the snippet above I still get a status text that says 'error', here is that snippet for reference:
$.ajax({
type: "POST",
url: "http://172.16.100.42/MobileRewards/api/devices",
data: {'EncryptedPassword':'1234','UserName':'test','DeviceToken':'d234'}
}).always(function( data, textStatus, jqXHR ) {
alert( textStatus );
});
So really, there are a couple of questions here.
1) Why can't I get any ajax calls (post or get) to successfully hit my End Point when it's hosted via IIS Express on the same machine that the Android VM is running?
2) When my end point is hosted on an actual server, through IIS and served through port 80, why can't I get post requests to be successful when I use jquery's ajax calls? (Even though I can get it to work by manually creating an XMLHttpRequest)
Thanks
Are you sure that BlueStacks uses the same host IP (10.0.2.2) as the emulator? I'm not familiar with it so I'm not sure what the answer to that is.
jQuery wants the data to be a string, try:
data: JSON.stringify({'EncryptedPassword':'1234','UserName':'test','DeviceToken':'d234'});
and for good measure, add
contentType: 'application/json',
in your ajax settings.
To anyone who ever looks this up, the issue ended up being the port that IIS Express was using on my local machine. When I got things to route through port 80, everything worked okay.

Can't access php file from other domain

I written a android mobile application in JQuery Mobile and PhoneGap in Eclipse. In the application I am calling a jquery ajax to load list of data from other domain.
My jquery ajax call code is:
$.ajax({
type: "POST",
url: WEBSERVICE_URL,
async: false,
data: dataString,
dataType: 'json',
crossDomain: true,
success: function(data) {
loginData = new Object(data);
hideActivityIndigator();
if(loginData.success == "true"){
$.mobile.changePage("#selectionScreen", "slide", false, true);
} else {
$("#message_ajax").html("Invalid UserName/Password.");
}
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
hideActivityIndigator();
}
});
The variable "WEBSERVICE_URL" has a other domain php service url.
On executing above code on "pageview" event I am getting following error
Error: NETWORK_ERR: XMLHttpRequest Exception 101
Any solution is there to access service from other domain in JQuery Mobile + PhoneGap...?
I think this might solve your problem.
If you are using Phonegap/Cordova you should be able to call cross-domain web-services.. are you using an emulator or a phone?
As for emulator I would recommend Ripple, you can add it as a Chrome extension or Download the standalone version

AJAX to Sharepoint Server with Phonegap and JQuery Mobile not working

I have the following Problem. In the Phonegap App(for Android) I want to make an AJAX-Call to connect with a Sharepoint Server, with the following Code:
$.ajax({
url:"https://xxx/_vti_bin/lists.asmx",
beforeSend: function( xhr ){
xhr.setRequestHeader(
"SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/GetListCollection"
);
xhr.setRequestHeader("Content-Type","text/xml; charset=utf-8");
},
dataType:"xml",
contentType: "application/xml; charset=utf-8",
timeout:10000,
type:'POST',
cache: false,
username: "username",
password: "password",
data: soapEnv,
success:function(data) {
// alert data
var serializer = new XMLSerializer();
serialized = serializer.serializeToString(data);
alert(serialized);
},
error:function(XMLHttpRequest,textStatus, errorThrown) {
// alert errors
alert("Error status :"+textStatus);
alert("Error type :"+errorThrown);
alert("Error message :"+XMLHttpRequest.responseXML);
alert("Error statustext :"+XMLHttpRequest.statusText);
alert("Error request status :"+XMLHttpRequest.status);
},
complete: function(jqXHR, textStatus){
alert(textStatus);
}
});
When I try to run it on the Android Emulator the error messages are:
Error status: error
Error type:
Error message: undefined
Error statustext: error
Error request status: 0
However when I try to run it on my Browser (Chrome) with disabled websecurity (because of same origin policy) it works all fine. Phonegap normally shouldn't care about SOP because of the file:/// Protocol. I added the following to 'mobileinit':
$(document).bind("mobileinit", function() {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
But when I run the same Code in Chrome without websecurity disabled, I get exactly the same errors as in the Android Emulator.
I also tried an AJAX call to wikipedia (with html instead of xml, and GET instead of POST), and that worked without a problem.
Also I think the AJAX to Sharepoint doesn't even get fired (no traffic in Fiddler2, if I managed to configure it the right way)
So I am really stuck with this problem since 2 days now, if anyone knows how to make this ajax call work, it would made me so happy :-)
(soapEnv is the XML envelope, sent to the server)
Well I know that once upon a time jQuery had a bug where it treated a request status of 0 as an error. When running from the file protocol a status of 0 is the same this as a 200 (OK). You may need to update your version of jQuery.
Alternatively to test my theory just do a plain vanilla XHR request to your service to see if it works. Here is my stock example:
http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

Categories

Resources