2
votes

Im trying to allow CORS on a PHP server I have the following code

<?php 
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
echo '<p>Hello World!!</p>';
?>

and a simple ajax request to fetch it using jquery

I keep getting this CORS error on my dev tools

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

I also used curl -i to check the response and im seeing it doesnt have the headers... what is wrong

curl -i

HTTP/1.1 200 OK Date: Mon, 31 Aug 2015 17:25:33 GMT
Content-Type: text/html
Content-Length: 97
Connection: keep-alive
Keep-Alive: timeout=30
Server: Apache/2
X-Powered-By: PHP/5.3.29
Cache-Control: private, pre-check=0, post-check=0, max-age=0
Expires: 0
Pragma: no-cache

2
@PaulCrovella i dont have * on the allow-headers , only on the Allow-origin which is ok , its not a duplicate - naoru
Check if the web server removes the header. - Charlotte Dunois
can you elaborate , im pretty sure it is - naoru
all of your headers are being ignored. note the content-type as well. Make SURE you haven't a "headers already sent" error situation. - Marc B

2 Answers

0
votes

What Charlotte Dunois commented gave the direction i added the following line in my .htaccess file

Header add Access-Control-Allow-Origin "*"
0
votes

I had a similar problem and the issue was the requested resource resided in a directory that had directory privacy applied to it by way of the .htaccess file residing in said directory. Removing the directory privacy allowed the php header to function as expected.