Phonegap + Webservice - android

I have a index.html with an ajax call to a webservice(ASP.NET) that works fine when i deploy it on the bigrock server(i have a domain).
$.ajax({
type: "POST",
url: "myurl/mywebservice.asmx",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("success");
},
error: function (msg,status) {
$('#demo').html("status is "+status+" . Msg is "+msg.d.responseText);
//attribute for responsetext might be wrong here but in my application it is correct as i am using different system ryt now.
}
});
But when i used the online zipper of phonegap to make a apk for my android app from the html, css,js,web.config,few class and installed it on my device...nothing comes up. Its shows me an ajax error with no responseText.
What am i missing?

When you zip up your assets to submit to PhoneGap Build, you need to include a config.xml file and specify access policy similar to
<access origin="https://your.domain.com" />
This will instruct PhoneGap to allow communication with your server.

When your app is deployed to the device using PhoneGap, your "Home" location becomes the file-system on the local device.
This means any ajax calls are cross-domain calls which require further configuration to setup correctly.
See this question for potential answer

Related

Basic user authentication works on Android, does not on iOS

I'm building an app that gets data from a server using basic user authentication. This works fine on Android, but on iOS nothing happens. In safari it also works fine.
This is the code I'm using:
$.ajax({
url: main_url+'api-cache/'+slug+'.json?r='+random_number(),
dataType: 'json',
headers: {
'Authorization': "Basic " + btoa(localStorage.getItem('email') + ":" + localStorage.getItem('password'))
}
})
.done(function(result) {
main_url is a link to an https website.
I have als tried username and passwork like the jQuery documentation says and also beforesend, nothing seems to work. Without the authentication it works fine by the way. Also the result seems to be ".done" because no error is fired in ".fail".
Help? Oh and btw, I'm using Phonegap Build.
Problem solved using a couple of headers on the server instead of changing the app: https://benjaminhorn.io/code/setting-cors-cross-origin-resource-sharing-on-apache-with-correct-response-headers-allowing-everything-through/

Cross-Origin Resource Sharing (cors) ajax request not working on mobile device

I am developing an app by using jQuery mobile and phonegap. Below are the versions of my phonegap, phonegap-1.1.0.js, jquery-1.7.1.js, jquery.mobile-1.4.1.min.js, From developing app I am calling Ajax request of other host having PHP website.
On that server I have set
header('Access-Control-Allow-Origin: *');
and in Js file defined
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
I am testing this app by using chrome browser on local system, Where everything is working correctly and I am getting responce. But when I generate build by using https://build.phonegap.com for android and ios. The cors Ajax request are not working
Below is the ajax code which I am calling
$.ajax({
type : "POST",
url : // Other server url
data : {
fId:'42'
},
dataType : 'json',
success : function(response) {
},
error : function(response) {
// Response is coming in this case
}
});`
Can anybody tell which settings I need to set
Thanks,
add the following line to your config.xml:
<access origin="*" />
http://docs.phonegap.com/en/3.4.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide

jQuery Mobile + Phonegap on Android - no Ajax

EDIT: Solution for JQuery Mobile + Phonegap AJAX Problems:
subdomains="true" property in config.xml is not working in phonegap 2.9.0, every request to a subdomain will return 200 but $.ajax won't fire the success function (and $.getJSON won't fire anyways).
Also: Syntax *.domain.tld as stated in the official doc is not working, only way I found to solve that problem: Setting access origin and acces uri to "*" (all)
<access origin="*"/>
<access uri="*"/>
I'm working on a mobile application using the jQuery Mobile Framework and Phonegap.
I've just added an AJAX request to my application which is used to get data from my webserver.
The page is correctly loading but Android (4.1.2) never fires the AJAX event or never gets a request from the Webserver, in fact it's just hanging on the loading spinner.
Strange thing about this is, that I already use an AJAX request to the same server (but with different domain) that works perfectly fine:
$.getJSON("[...]checkupdate.php?callback=?", function(data){
[...]
})
I've tried to do the second request with $.getJSON too and it worked on my computer but failed on Android. I started looking around for a solution and found out, that setting the cache to false in $.ajax might help so I rewrote my code but it's still not working.
$.ajax({
cache : false,
type: 'GET',
url: requestURL,
dataType: 'jsonp',
context: document.body,
success: function(data){
[...]
}});
Is there anything else to do to get this to work?
I found a way to solve that problem. I noticed, that the subdomains="true" property in phonegap's config.xml is not working. (Phonegap 2.9.0). Every request to a subdomain will return status 200 but $.ajax won't fire the success function (and $.getJSON won't fire anyways). The syntax *.domain.tld as stated in the official doc isn't working for me either, only way I found to solve that problem was setting access origin and acces uri to "*" (all)

phonegap application ajax calls fail after upgrading android from 2.3.3 to 4.0.3

I have developed a mobile application using :
jquery mobile 1.1
jquery 1.7.2
cordova/phonegap 2.0.0
this application fires AJAX post calls to a remote server, something like:
$.ajax(
type: "post",
cache: false,
timeout: 30000,
url: "http://"+ username+":"+password +"#mycompany.com/mysite/and/so/on.asmx",
contentType: "text/xml",
// other params...
The application works fine in Android 2.2 and 2.3.3. So far so good.
The users have upgraded to Android 4.0.3, the main page loads fine, but ajax calls don't work anymore.
Also in the emulator of android 4.1 it's the same.
Considering nothing else has changed but the platform, what could have changed in the Webkit layer to cause the problem?
Are there known migration rules to be followed?
thank you
Quite a lot of things are more locked down in newer versions of android to improve security. Passwords included in the url are insecure, especially if you don't use https, so some browsers no longer support them.
I haven't seen anything specific to android webview, but it is definitely deprecated in google's other browser.
http://code.google.com/p/chromium/issues/detail?id=123150
Try setting the credentials in a header instead:
$.ajax({
type: "GET",
url: url,
dataType: 'json',
async: true,
data: {},
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', "Basic " + btoa(user + ':' + pass));
},
success: onSuccess,
error: onError
});
Just put <access origin="your server" subdomains="true" /> to config.xml and you are done. No need to rewrite all ajax calls ;-) Just done it with my application and its working fine on all android versions.

phonegap android ajax requests work for GET but not POST

Sending the below request in my phonegapp-ed android app works for GET but not POST.
With GET, everything works. With POST, the request goes through but the POST variables
are not coming through on the server side, and the server returns a
json response that says 'no parameters supplied.'
POST works fine from our mobile app - it is just the phonegap app where we are having an issue. What am i missing here??? Thanks in advance for any help you can provide!
I've tried changing the settings on the $.ajax call, the android manifest, everything I can think of.
Also, i'm using Android 2.2 and Phonegap 1.0
function goTeam(){
var dataString={lat:currentLocation.lat(),lng:currentLocation.lng()}; // this all works
$.ajax({
url: 'http://example.com/request/goTeam',
data: dataString,
dataType: 'json',
success:
function(b) {
if(b.status==1){ // woo hoo! it works
} else {
// the request went through but something was wrong - this is what i'm getting with POST
}
},
type: 'post', // works with GET, doesn't work with POST
error: function(jqXHR, textStatus, errorThrown){ alert("Noooo."); }
});
Are you trying cross-domain requests? Only GET requests work this way. You can use JSONP for this kind of request, but only GET works.
Phonegap does work with both GET and POST - cross-domain security issues do not apply. We had an idiosyncratic error our code that was preventing it from working. Phonegap is pretty awesome!

Categories

Resources