I'm currently developping a phonegap app with backbone but i'm not able to fetch my data from a cross domain website.
Testing the app on android gives me no results but testing it in chrome with "chromium-browser --disable-web-security" gives me a success response with the data I need.
Using phonegap I added the following lines of code:
config.xml
<access origin=".*"/>
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
json
[
{
"id":"918",
"merk":"Yamaha",
"type":"YZF R1"
...
Its a valid json file.
My backbone collectionName.fetch() doesn't work, and doing a simple ajax call isn't working either inside the app.
Is there any reason why I cannot fetch my data from the api in my phonegap android app!
I really could use some help
cheers!
From the file:// protocol there should be no limitations regarding same-origin, see here: http://en.wikipedia.org/wiki/Same_origin_policy#Corner_cases_and_exceptions
However it could be the server doesn't allow access to requests originating from file:// url's, check your server log. To doublecheck you could also access a publicly accessible webservice like Google Geocoding (see comment).
Edit: this is actually wrong...
This likely is because cross domain is only allowed within the same protocol. Since your phonegap app runs from file:// this introduces problems when connecting via http(s):// protocol.
Related
I have a small app to test:
https://github.com/vmbelizario/phonegap
its just a login form who connects to a PHP page and check if the login exist.
The side-server is the auth.php file.
The client-side is the js/auth.js who connect to http://myserver.com/auth.php (example) and do the login test.
If i upload in a apache + mysql server, works.
But in android, after put the credentials i got only "connecting..." and didnt worked.
I tested via adobe builder (build.phonegap.com/apps) build and install in my smartphone and via monaca.io android emulator.
Can someone check what is wrong and help me?
Thanks!
Make sure you have the Cordova Whitelist plugin installed.
cordova plugin add cordova-plugin-whitelist
This is required for Android and not on iOS or Browser.
Then check your config.xml for lines like these.
<access origin="*"/>
This will allow any network communication from your app. To be more safe you can change this to.
<access origin="YOUR_URL/*" />
This will allow network access only the the URL specified and any subdomains.
See the documentation here.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/
I think you need to use the query string in the url. Try to concatenate the URL with your data string and pass it as a complete url in your ajax call. First try to hit the complete URL with static values in the browser, if it works then that's a solution for your problem. For example: http://myserver.com/auth.php?callback=?email=example#mail.com&password=password&login=
I am building my first PhoneGap application, just a proof-of-concept at the moment.
What I want to do is to perform an Ajax call (using JSONP) to retrieve a record from a database on a backend server. The code is working perfectly in the browser, but when I make an Android .apk using PhoneGap Build, that call is not doing anything.
The pages opens, and local Javascript works. I can even use localStorage.
But it seems like the application is not allowing my to communicate out to the Internet.
In config.xml I have added
<access origin="*"/>
as well as
<config-file platform="android" parent="/manifest" mode="merge">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</config-file>
Here is the relevant jQuery code which does the call:
var json = new Object();
var docunid = $("#policyNumber").attr("data-docunid");
$("#statusInfo").html("Loading " + docunid + "...");
json["unid"] = docunid;
$.ajax({
url: "http://www.example.com/websites/losscontrol/ajax_GetInspectionDetails.jsonp",
dataType: "jsonp",
data: json
});
When the code is executed, the message "Loading xxxxxxxx..." is being displayed, then nothing. In the browser it all works, the data is retrieved and the values displayed by the callback function.
What am I missing?
Update:
Thanks to Simon's comment I managed to solve it. The network guys had blocked the web server from external access without me knowing it.
So when I executed the code in the browser on my computer inside the firewall, it worked, but when the same URL was called from the PhoneGap application on my phone (and therefor outside the firewall) it simply did not work.
If you haven't already done so, you need to install the Cordova whitelist plugin. This issue had me stumped for a while. There are other threads on here with the same problem.
I created my first Sencha touch 2 app by watching this video (http://youtu.be/5F7Gx0-W-M4) and it has a store page structured like this:
Ext.define('FirstApp.store.Places',{
extend:'Ext.data.Store',
config:{
autoLoad:true,
model:'FirstApp.model.Place',
proxy:{
type:'ajax',
url:'https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyCFWZSKDslql5GZR0OJlVcgoQJP1UKgZ5U',
reader:{
type:'json',
rootProperty:'results'
}
}
}
})
The after-build (after running "sencha app build native") package work very well on my MAC (all browsers) but the generated app (i am running it on my nexus phone) works but doesn't collect any data from the google maps json.
Any help would be appreciated
The example you are referring is using google map's place search API. You can not use this API when you build the app for mobile phone with proxy set to ajax . Basically, you can not use any resource that is outside your domain. Like if your site is at yourdomain.com and there is someotherdomain.com, then you can't make ajax request to this someotherdomain.com from yourdomain.com unless that domain allows you to. In this case, your mobile app is not having any domain. You are just loading a page inside webview.
The reason is, ajax will not be able to load cross-origin resources. App build works on browsers because I believe you're using chrome with --disable-web-security flag. To work with CORS you need to use JsonP proxy. It's the only way if you're packaging for mobile app. If in a case, you own the server ( not in this context though ) then you can allow CORS by setting appropriate headers like
Access-Control-Allow-Origin: *
or
Access-Control-Allow-Origin: http://yourdomain.com/resource
Try setting proxy to JsonP .
I am a newbie in android. Can anyone help me in running my asp.net web service in android application. I tried using SOAP OBJECT but the application force closes. The web service is the simple one which takes my name and returns HELLO + "my name". HELP me in code.
You can use this is library to full fill you need
This is Tutorial telling how to use this library to call webservice
Note : Source code also available
don't forget to give internet permission on manifest file
<uses-permission android:name="android.permission.INTERNET"/>
Refer : See Dummy Class calling SOAP method
im testing this on nexus s with ics
phongap version 1.4
jqm version 1.0.1
i tried following these guidelines,
1) added Access-Control-Allow-Origin in server app running at local ip http://192.168.1.132/orderpage, tried with both html and json reponses
2) may be this not needed but still i've set $.support.cors and $.mobile.allowCrossDomainPages to true;
3) phonegap.xml origin is set to *
4) also put a super.setIntegerProperty("loadUrlTimeoutValue", 60000);
this works fine on browser but i keep getting error loading page on phonegap no matter what!
am i missing something in phonegap app? loading local file works fine $.mobile.loadPage('order.html') but
$.mobile.loadPage( "http://192.168.1.132/orderpage", { showLoadMsg: true } );
fails with error loading page
the app im trying to build is totally depended on server side content, how can i safely load external content with ajax calls or loadpage()
and how to debug to check if phonegap is even making the request to network?
any suggestion is greatly appreciated.
thank you,
I found I need to add <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
To my AndroidManifest.xml
HTH