Railsgrammer

Never a fun error: Access-Control-Allow-Origin

by coffeencoke on Jan.19, 2012, under Development

If you are testing any javascript app against a web service and you run into the following error in your javascript console:

XMLHttpRequest cannot load http://localhost:3000/. Origin http://myapp.dev is not allowed by Access-Control-Allow-Origin.

In this context it means that the host your are making the request from does not have permission to access the requested resource (it also means a lot of other things).  After hunting you can do a at least 2 things:

1. Add the host to the access list by adding to the request header (http://enable-cors.org/)

2. Load the app using file:/// rather than the host.

Because I was writing this app as a native mobile app for iOS, Android, etc. I found out that PhoneGap loads the html files using the file:/// protocol.  So now, when I am developing on my computer, as long as I use the file:/// protocol, I have no problem with access to my API.

:, , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...