How to set CORS header?

I have been accessing media images in a Ruby on Rails application via ajax from wordpress 4.2.4.

I am getting following error while accessing the image via ajax.

XMLHttpRequest cannot load http://myblog/wp-content/uploads/2015/11/test.jpeg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stage.mysite.com' is therefore not allowed access.

I tried WP-CORS plugin and set * as allowed domains but it doesn’t work. I verified that the response doesn’t have Access-Control-Allow-Origin header.

I am not wordpress/php guy so would appreciate for simplest possible solution.

1 Answer
1

Fixed it by adding Header set Access-Control-Allow-Origin "*" in apache2.conf:

<Directory /var/www/>
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
  Header set Access-Control-Allow-Origin "*"
</Directory>

Leave a Comment