CaptainCarl
CaptainCarl

Reputation: 3489

$.GET to localhost in Rails. Temporary solution for 'Access-Control-Allow-Origin'?

I'm creating a little frontend that gets .json files from my rails application. I've got the frontend detached from the application. Because that's how it will work when it will go online.

When it will be online, however, the domain will be the same; Thus there's no crossdomain call problem at that point. Now, in development on localhost, there is.

I know of gems like Middleman and such. Is that the way to go to avoid errors like XMLHttpRequest cannot load http://localhost:3000/groups/1/activities.json. No Access-Control-Allow-Origin header is present on the requested resource. Origin 'null' is therefore not allowed access.

Or is there a short workaround which I can use while I'm in development mode?

Upvotes: 1

Views: 764

Answers (2)

Cris
Cris

Reputation: 13351

you should have following headers added on the backend side

headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'

Upvotes: 1

Peter Goldstein
Peter Goldstein

Reputation: 4545

You can use rack-cors - https://github.com/cyu/rack-cors . Takes about 10 minutes to set up.

Upvotes: 1

Related Questions