7

How Ajax call to server page works prefect in mobile app (using PhoneGap). But, When I run the same Ajax call from normal HTML page in PC browser it is not working. I guess the problem is cross domain. But, how it's work good in mobile app.

Using JSONP it is working fine in pc browser and mobile app (using PhoneGap). But in mobile app without datatype="jsonp" also working fine.

Naga Harish M
  • 2,779
  • 2
  • 31
  • 45

4 Answers4

12

From the PhoneGap FAQ:

Q. I want to create an application for phonegap to access externally deployed web services via AJAX. How can i resolve the issue with the cross-domain security policy of XmlHttpRequest?

A. The cross-domain security policy does not affect PhoneGap applications. Since the html files are called by webkit with the file:// protocol, the security policy does not apply. (in Android,you may grant android.permission.INTERNET to your app by edit the AndroidManifest.xml)

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139
  • Thanks for you ans. But, in PC browser also we are use file:// protocol (just open HTML page in browser). it is not work working. If cross-domain security policy does not affect file://protocol . Then Please provide me some information about that (may be just post link). – Naga Harish M Sep 17 '11 at 05:02
  • 2
    The cross-domain policy in the browser is still enforced for local files. You will *not* be able to make the cross-domain request from the browser. – Jason Dean Sep 17 '11 at 15:13
  • 2
    Also, the different browsers do not have the same cross-domain policy for local files See also http://stackoverflow.com/questions/3988494/cross-domain-ajax-fails-even-for-a-local-file – Paul Beusterien Sep 17 '11 at 15:37
7

Mobile applications (PhoneGap, Adobe AIR, Titanium, Native, etc) do not have the same-origin restriction for XHR requests that desktop browsers do. They can make any cross-domain calls that they want. That is why it works fine on the phone and not on the desktop.

JSONP works fine in both because it does not use XHR. It does a cross-domain request by use the <script> tag. So it gets around the same-origin policy of the browser. JSONP can only work though if the remote service returns the data in the form of a function that is then immediately executed.

Jason Dean
  • 9,585
  • 27
  • 36
1

I guess all WebView based views has no restriction rules about cross-domain XHR requests. Another way to a cross-domain request is a server-side bridge.

Nick Nizovtsev
  • 233
  • 1
  • 10
0

you can use iqouta to make ajax call in phonegap search for iqouta it easy to use !!

htmlove
  • 9
  • 1