Tricks for Debugging PhoneGap Applications in Desktop Browsers
Join the DZone community and get the full member experience.
Join For Freei often encourage people to develop as much as possible in desktop browsers when they are building phonegap applications. while there are remote debugging tools such as winre for remote debugging on devices, the developer experience is still better inside of a desktop browser b/c it gives you breakpoints and memory inspection capabilities. in most cases, you can develop the vast majority of your application within the desktop browser, and switch to a physical device when integrating with phonegap apis or for performance testing. personally, i use chrome , and take advantage of the chrome developer tools .
however, when developing from the local file system, you will run
into a few roadblocks with desktop browsers. a big issue is the browser
security model. if you try and asynchronusly request files when the
application is loaded from the local file system, you’ll like get an
error like:
xmlhttprequest cannot load (filename or url). origin null is not allowed by access-control-allow-origin.
luckily, there are a few tricks you can use to get around this… local http servers and relaxing the browser’s security.
note: i’m on a mac, and i don’t know the syntax for these on windows.
local http server
the first option is to use a local web server to host files. you
can fire up a simple http server from any directory in osx by opening a
terminal and using the following command:
python -m simplehttpserver 8000
an http server will start, and you’ll be able to access any content
from that directory and child directories from the url
http://localhost:8000/ thanks to
@mesh
for this trick.
however, this will only work for static content that is within your
directory structure. if you need dynamic content, you’ll need some
sort of application server infrastructure.
relaxed browser security
ok, that’s great, but it doesn’t cover every situation. what if you don’t want a local application server configuration? what if you want to develop against services that are remote, and you don’t control them? well, you are in luck.
you can disable security restrictions in chrome to allow you to
access these services. the following command will allow unrestricted
access from the file system, and will also allow cross-site requests.
kill chrome, then re-launch from a terminal window with the following
flags:
open -a /applications/google\ chrome.app –args –allow-file-access-from-files –disable-web-security
using this configuration, you’ll be able to open an html file from
the local file system, and that file will be able to make requests to
other sites to access data or other resources.
in the screenshot below, you can see that i launched my census browser application from the local file system, and it is successfully able to access services from http://www.tricedesigns.com . with the default browser security policy, this would not be possible.

local application debugging with remote services
warning: only use this for debugging!
do not use this configuration for normal web browsing b/c it will leave your browser security wide open, and able to be exploited/compromised. do not browse the web when you’ve relaxed the security policy.
thanks to fellow adobe evangelist
piotr walczyszyn
for this trick.
source:
http://www.tricedesigns.com/2012/03/02/tricks-for-debugging-phonegap-applications-in-desktop-browsers/
Opinions expressed by DZone contributors are their own.
Comments