我试图通过AJAX发送POST请求到一个单独的子域。 预检请求(OPTIONS)是成功的,但下面的XMLHttpRequest请求返回“原点http://app.example.com不受访问控制允许来源允许”。
该客户端(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个状态。 我希望有人能告诉我,我错过了什么。 谢谢!