Can PhoneGap support CORS?

4,636 views
Skip to first unread message

Eugene Mirkin

unread,
Jun 2, 2012, 12:15:41 PM6/2/12
to phonegap
Anyone who ever tried using 3rd party REST services via AJAX may know
about the restrictions on cross-domain scripting. That is the browser
disallowing JS-originated HTTP JSON requests for fear of a variety of
unsound reasons. A common workaround is to implement CORS (cross-
origin resource sharing) headers. While that works well in the pc
domain, most mobile browsers disallow it.

Phonegap is in a unique position to help us all (us - all those trying
to build applications that call REST APIs from JS). Since every
Phonegap app comes with a native component, it would be really cool if
Phonegap offered JS AJAX tied to a Java/Obj-C native handler. I.e.
rather than doing AJAX through the browser and falling victim to its
restrictions, one could submit their requests through the native stack
which does not have the same limitation. What say, Phonegap?

Noli Sicad

unread,
Jun 3, 2012, 4:49:15 AM6/3/12
to phon...@googlegroups.com
PhoneGap for iOS, Android and BlackBerry use WebKit. WebKit supports CORS.

"WebKit: as announced on the WebGL mailing list, CORS support for
images and videos is now fully implemented"

http://enable-cors.org/

Most of the UI for PhoneGap (e.g. jQTouch, jQuery Mobile, Sencha
Touch, etc.) use AJAX-JSON to post/get data from web servers.

Noli
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Julien Chaumond

unread,
Jun 4, 2012, 4:37:19 AM6/4/12
to phon...@googlegroups.com
Hi Eugene, Noli,

In case the server you're pulling data from doesn't support CORS, it would be very easy to do what you describe through a Phonegap plugin (see for example the Twitter plugin for iOS where you pass a URL and parameters stored in a JS array and get a JSON string as output -- https://github.com/phonegap/phonegap-plugins/pull/595)


Julien

filmaj

unread,
Jun 4, 2012, 2:07:17 PM6/4/12
to phonegap
Also, in PhoneGap you can just XHR directly to remote servers and it
should "just work". Cross-domain policy does not apply to PhoneGap
(for a variety of reasons, basically because your app is essentially
running off the file:// URI on-device).

Please be aware that you will have to set up a whitelist for your apps
to access these external domains.
http://docs.phonegap.com/en/1.8.0rc1/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide

On Jun 4, 1:37 am, Julien Chaumond <chaum...@gmail.com> wrote:
> Hi Eugene, Noli,
>
> In case the server you're pulling data from doesn't support CORS, it would
> be very easy to do what you describe through a Phonegap plugin (see for
> example the Twitter plugin for iOS where you pass a URL and parameters
> stored in a JS array and get a JSON string as output --https://github.com/phonegap/phonegap-plugins/pull/595)
>
> Julien
>
>
>
>
>
>
>
> On Sun, Jun 3, 2012 at 10:49 AM, Noli Sicad <nsi...@gmail.com> wrote:
> > PhoneGap for iOS, Android and BlackBerry use WebKit. WebKit supports CORS.
>
> > "WebKit: as announced on the WebGL mailing list, CORS support for
> > images and videos is now fully implemented"
>
> >http://enable-cors.org/
>
> > Most of the UI for PhoneGap (e.g. jQTouch, jQuery Mobile, Sencha
> > Touch, etc.) use AJAX-JSON to post/get data from web servers.
>
> > Noli
>
Reply all
Reply to author
Forward
0 new messages