Phonegap / Converting website for mobile use
Currently I am building an app using phonegap fo开发者_如何学JAVAr the Android and iOS systems. Essentially, it will be a website but I am running into some difficulties due the cross domain requests I need to make via ajax (same origin policy). Im wondering if its better to make the website on my own servers, where PHP is allowed, and then use a wrapper/frame in phonegap to emulate the site? How would that work?
Help appreciated
You shouldn't be having this problem at all. PhoneGap apps are loaded on the device as local file:// pages, and the cross-domain security policy does not apply to them.
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)
If you are having issues with cross-domain requests then consider using something like jsonp as the data interchange format. Where are you requesting the data from?
If I understand correctly, you want to create a PHP proxy for a cross domain service so that you can access it with your mobile app using phonegap? This is a pretty common thing, its done a lot in Flash as well to get past cross domain restrictions.
For one of my demos I need to access Google Images from Flash. To do so I created a VERY simple PHP proxy on my server called imageproxy.php. Here's the complete code:
<?php
readfile($_POST['url']);
?>
Yep, thats its. So in your case, if you were using this PHP proxy on your server, you would send this proxy your target URL as a post variable and the proxy makes the request and returns the response via readfile().
精彩评论