Tuesday, July 12, 2011

python SimpleHTTPServer solves XMLHttpRequest errors

Loading an html file in a browser from the filesystem (i.e. file:///) that uses XMLHttpRequest will throw a javascript error since for security purposes file:/// requests have a null origin.

Example error:
XMLHttpRequest cannot load file:///somepath. Origin null is not allowed by Access-Control-Allow-Origin.

The solution is to serve the html file using http from a local webserver.  A very easy and simple webserver to run is offered with python by typing this in the folder you want to serve files from:
python -m SimpleHTTPServer 1234

Then navigate to http://127.0.0.1:1234/somepath to view the file.

No comments:

Post a Comment