Padfoot
Padfoot

Reputation: 155

How to resolve Access-Control-Allow-Origin error?

I know there are lot of queries already posted related to this issue. But I am still not able to resolve the error.

I am getting,

XMLHttpRequest cannot load http://localhost:100/test/test1.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

To resolve I tried following -

  1. I used --allow-file-access-from-files --disable-web-security, while launching chrome.exe. (Not working)
  2. I put header('Access-Control-Allow-Origin:*'); in php, after <?php tag. (Sometimes it works, but not always).
  3. In ajax, $.ajax({crossDomain:true}) I have included. (Not working).

Upvotes: 1

Views: 5214

Answers (3)

John Green
John Green

Reputation: 13435

There's a manual:

http://enable-cors.org/

This presents a variety of options depending your server setup (as well as explain the topic fairly well).

Note that the previous answers are totally correct for PHP... header('Access-Control-Allow-Origin:*'); if you're having problems, remember that you need to send that header before you send any body output to the buffer.

Upvotes: 1

FuRioN
FuRioN

Reputation: 633

Add these three lines:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

Upvotes: 2

Akash Shinde
Akash Shinde

Reputation: 955

Simple solution to get this done is you need to add header to server side

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

In one of my golang API server,I added below header code to allow CORS

Response.Header().Set("Access-Control-Allow-Origin", "*")

Upvotes: 0

Related Questions