JetThoughts

Allow multiple access control requests for Rails

December 22nd 2010

If you have problem with sending XHR requests from different domains, you find trouble to get content, because

XMLHttpRequest cannot load http://different.domain.local:3000/visits. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

The solution is to setup response headers: Access-Control-Request-Method, Access-Control-Allow-Origin.

1 headers['Access-Control-Allow-Origin'] = '*'
2 headers['Access-Control-Request-Method'] = '*'

Example for rails:

1 #app/application_controller.rb
2 after_filter :set_access_control_headers
3 
4 def set_access_control_headers
5   headers['Access-Control-Allow-Origin'] = '*'
6   headers['Access-Control-Request-Method'] = '*'
7 end
blog comments powered by Disqus

Powered by Rackspace Cloud Computing