1

I have such code on my php server (5.2) endpoint GET http://mydomain/get-code.php:

<?php
    header("Access-Control-Allow-Origin: *");

    $data = 'id=' . '123456' . '&' .
            'text=' . 'some text' . '&' .
            'code=' . urlencode($_GET['code']);
    $ch = curl_init('https://someapi.com/login/oauth');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    preg_match('/hash=([0-9a-f]+)/', $response, $out);
    echo $out[1];
    curl_close($ch);
?>

I'm requesting GET http://mydomain/get-code.php from another domain.

I'm getting CORS error in browser console:

XMLHttpRequest cannot load http://mydomain/get-code.php No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9292' is therefore not allowed access.

Any ideas?

20
  • Doesn't this header should come from the server side? Commented Jan 28, 2016 at 21:27
  • What is your issue ? cors is not related with the client. Commented Jan 28, 2016 at 21:28
  • enable-cors.org Commented Jan 28, 2016 at 21:30
  • I'm requesting GET mydomain/get-code.php from another domain. Commented Jan 28, 2016 at 21:31
  • @AlonEitan thanks, I've added header("Access-Control-Allow-Origin: *"); but this didn't help Commented Jan 28, 2016 at 21:32

1 Answer 1

4

Access-Control-Allow-Origin is for the server that is receiving the request, not the client sending the request.

Sign up to request clarification or add additional context in comments.

2 Comments

You would need to add this to the mydomain/get-code.php page: header("Access-Control-Allow-Origin: *");
You have to add it to the page that is being requested (the server), not from the client (where you are using curl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.