1

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

How to resolve it. i added following headers in that php file:

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST, OPTIONS');

header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, 
Authorization, X-Request-With');

header('Access-Control-Allow-Credentials: true');

i replace my domain name 'codeXXX' with 'example' dont confuse with that

7
  • you have to add CORS support in server config, more details on CORS developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Commented Aug 30, 2017 at 5:11
  • @Nemani it looks like Venkata has done that Commented Aug 30, 2017 at 5:11
  • Make sure this is right at the top of your .php script so that nothing it output before these headers are sent Commented Aug 30, 2017 at 5:16
  • 1
    Also, check your browser's Network console. is there an OPTIONS request? What do the response headers look like for the failed request? Commented Aug 30, 2017 at 5:18
  • Possible duplicate of "No 'Access-Control-Allow-Origin' header is present on the requested resource" Commented Aug 30, 2017 at 5:18

1 Answer 1

1

The header

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

MUST be given in response to an OPTION request to "http://example.com/test.php". If OPTION request contains this header, the following GET (or POST) will be accepted.

If the browser says that the header "Access-Control-Allow-Origin" is not present, ... just add it ^_^

header('Access-Control-Allow-Headers: Access-Control-Allow-Origin, Origin, Content-Type, Accept, Authorization, X-Request-With');
Sign up to request clarification or add additional context in comments.

5 Comments

I feel like OP would have a different error message if the server weren't responding correctly to an OPTIONS request.
And the OP actually have OPTIONS in the allowed methods, you don't ?
You have two request. First one is OPTION that tells to your client how can be the second.
ANY time you make a request with angular, the request is preceded by an OPTION request.
@sensorario that's not correct. A standard GET request without authorization will be simple and not require a pre-flight request. A POST request using Content-type: application/x-www-form-urlencoded is also excempt

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.