I know that the jQuery .load() function has a "problem": You can't retrieve pages that are outside of the current domain, because of the Same Origin Policy, but I remember when I was developing another program that I could do cross-domain AJAX without problems while on an PhoneGap compiled environment, but will it work while on PhoneGap(like normal AJAX) or it will just fail because of the policy?
2 Answers
You can use .load()
or $.ajax()
in PhoneGap applications. Most of my experience is with getting information from the same domain under which the app. is packaged. For example:
App. package identifier:
com.my-domain.my-app
Website domain for ajax calls:
www.my-domain.com
I just did a simple test in an iPhone emulator (via Xcode) and I was able to get the contents of a personal web domain as well as http://www.google.com/. Test was as follows:
$(document).ready(function () {
$.get('http://www.google.com/', function (data) {
alert(data);
});
});

- 28,769
- 59
- 194
- 300

- 75,717
- 14
- 151
- 146
-
Thanks very much for helping me and testing it **:)** – Nathan Campos Aug 23 '11 at 00:25
-
1No problem, your question peaked my interest. If I find any more information regarding cross-domain ajax calls from native applications I'll post info/links here. – Jasper Aug 23 '11 at 00:28
-
As far as I understand the reason for no cross-domain problems with PhoneGap is that the origin of the JS file is local filesystem - and for this source no additional restrictions are applied. – NPC Aug 23 '11 at 18:42
-
I also did the test and realized that it works, but the question remains why it works. It would be great if somebody could explain. – Tim Büthe Apr 05 '12 at 21:03
-
Additional info, Same origin policy also affects localhost and "file://" pages. – Tim Büthe Apr 05 '12 at 21:03
-
1@TimBüthe My understanding is that the app on a device has no domain, so it has no method of checking if the request is on a different domain. Some platforms can set some jQuery settings to disallow cross-domain-access even though it's available: http://jquerymobile.com/demos/1.1.0-rc.1/docs/pages/phonegap.html – Jasper Apr 05 '12 at 21:22
I tried doing this, testing on the ipad simulator and it didnt work for me, I kept getting cross-domain ajax errors (i.e. permission errors).
I eventually found that I needed to Navigate to [projectName] -> Support Files -> phonegap.plist Under External Hosts add a new string with the value * Or add your exact domain
(From this link thanks to Dror 'Yitzhakov).