No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access

Th3lmuu90 picture Th3lmuu90 · Dec 6, 2013 · Viewed 542.1k times · Source

I'm using .htaccess to rewrite urls and I used html base tag in order to make it work.

Now, when I try to make an ajax request I get the following error:

XMLHttpRequest cannot load http://www.example.com/login.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

Answer

Jay Patel picture Jay Patel · Jun 18, 2014

Use addHeader Instead of using setHeader method,

response.addHeader("Access-Control-Allow-Origin", "*");

* in above line will allow access to all domains.


For allowing access to specific domain only:

response.addHeader("Access-Control-Allow-Origin", "http://www.example.com");

Check this blog post.