Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Ajax call is working in mobile app (PhoneGap), but not in PC browser?

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.

like image 672
Naga Harish M Avatar asked Sep 16 '11 05:09

Naga Harish M


2 Answers

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)

like image 151
Paul Beusterien Avatar answered Nov 04 '22 22:11

Paul Beusterien


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.

like image 7
Jason Dean Avatar answered Nov 05 '22 00:11

Jason Dean