http - PHP Cross-Origin-Request blocked

999

I have a PHP-script which takes a POST-variable from a request, does something with it (in this case it's an memcached request for the specific key) and sends it back via JSON.

The complicated part is that I don't host this script on the same Server as I do with the script doing the request. So I tried to use CORS. My browser tells me that I haven't set the Access-Control-Allow-Origin-Header, but I did as you can see. So I guess that I set it wrong. But I haven't really dealt with headers in PHP so I have really no clue what do do. I already searched (also on Stackoverflow) but all I found is that I have to set this header, what I did...

Of ourse the memcache-part isn't really interesting for solving the problem but I thought it would be better to let you guys take a look at the complete code, so here you go:

<?php
header("Access-Control-Allow-Origin: * ");
$key = $_POST["key"];
$json = "";
if($key != "")
{

    define('MEMCACHED_HOST', '127.0.0.1');
    define('MEMCACHED_PORT', '11211');
    $memcache = new Memcache;
    $memcache->connect(MEMCACHED_HOST, MEMCACHED_PORT);

    $value = memcache->get($key);

    $json_array = array("status" => "done" , "key" => $key, "value" => $value);
    $json = json_encode($json_array);
}
else
{
    $json_array = array("status" => "empty");
    $json = json_encode($json_array);

}
echo $json;
?>
375

Answer

Solution:

Inserting some lines into the Apache .conf file did it. If someone is interested, these lines can be found here. I still don't know why it didn't worked with the headers in PHP. Maybe I did something wrong with the client. But it's working now :)

People are also looking for solutions to the problem: php - Wordpress Get the CURRENT Category from Single Post, Multiple Categories

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.