ajax 経由で別のサブドメインに POST リクエストを送信しようとしています。プリフライト リクエスト (OPTIONS) は成功しますが、次の XMLHttpRequest リクエストは「Origin http://app.example.com is not allowed by Access-Control-Allow-Origin.」を返します。
クライアント側 (app.example.com) のコードは次のようになります。
var settings = {
url: 'http://api.example.com/auth',
type: 'POST',
contentType: 'application/json',
crossDomain: true,
headers: {"X-Requested-With": "XMLHttpRequest"},
username: data.username,
success: callback,
error: callback
};
$.ajax(settings);
サーバー側のコード (api.example.com) は次のようになります。
$this->output->set_header('Content-Type: application/json; charset=utf-8');
$this->output->set_header('Access-Control-Allow-Origin: http://app.example.com');
$this->output->set_header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS');
$this->output->set_header('Access-Control-Allow-Headers: X-Requested-With, Origin, X-Csrftoken, Content-Type, Accept');
$this->output->set_header('Access-Control-Allow-Credentials: true');
OPTIONS リクエストは 200 ステータスを返します。誰かが私に欠けているものを教えてくれることを願っています。ありがとう!