I have two local rails apps that I would like to talk to each other for testing purposes… one is running on port 3000 and the other on 9292.
But when I make an ajax request from localhost:3000 to localhost:9292 I keep getting this issue:
Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
Any idea on how to fix this?
I am using a simple Sinatra app to receive (for testing purposes ONLY) JSON requests. Below is how I got two rails apps talking to each other on localhost (one on port 3000 and the other on port 9292)
Working Code
before do
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, X-CSRF-Token'
end
after do
headers['Access-Control-Allow-Origin'] = 'http://localhost:3000/'
end
Hope this helps!
Cross-domain AJAX is generally not allowed for security reasons. JSONP is an option if you are able to use it. If not, you can use something like flXHR to get around this restriction.
Best of luck!