Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin

Tags:

ajax

php

cors

api

I am trying to login with ajax to an API and I get this error:

XMLHttpRequest cannot load. The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin. Origin 'http://localhost' is therefore not allowed access.

I read all about this error, all over the internet, and I've tried all the solutions I could find online. I modified the .htaccess and apache httpd configuration file according to the CORS instructions here: http://enable-cors.org/server_apache.html

Access-Control-Allow-Origin: *

Nothing seems to be working. I'd really appreciate if you guys can help me out with this. Thank you!

like image 380
Daniel R. Avatar asked Jun 26 '16 21:06

Daniel R.


People also ask

What should be the value of Access-Control allow origin?

The specification of Access-Control-Allow-Origin allows for multiple origins, or the value null , or the wildcard * . However, no browser supports multiple origins and there are restrictions on the use of the wildcard * .

How does Access-Control allow Origin header work?

Access-Control-Allow-Origin is a CORS (cross-origin resource sharing) header. When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Origin response header to tell the browser that the content of this page is accessible to certain origins.

How do I fix Access-Control allow Origin error?

Configure the CloudFront distribution's cache behavior to allow the OPTIONS method for HTTP requests. If you still see errors after updating your CORS policy and forwarding the appropriate headers, allow the OPTIONS HTTP method in your distribution's cache behavior.

What happens if Access-Control allow origin is not set?

So, What is This Error Then? This error occurs when a script on your website/web app attempts to make a request to a resource that isn't configured to accept requests coming from code that doesn't come from the same (sub)domain, thus violating the Same-Origin policy.


1 Answers

You have to set Access-Control-Allow-Origin header to * or specified value http://localhost

You can do this through:

1- Your code

2- .htaccess file

3- Server config (restart web server required)

Here is the link that show how to do it on apache

http://access-control-allow-origin-guide.com/enable-cors-on-apache-linux/

like image 66
Dzung Cao Avatar answered Oct 18 '22 18:10

Dzung Cao