我意识到这个问题已经被问过十几次了,每个回答都表明我做得对,但也许我错过了一些东西.
AJAX像这样提供CORS请求......
$.ajax({
url: 'someotherdomain.com',
type: 'post',
data: {key: 'value'},
dataType: 'json',
async: false,
crossDomain: true,
beforeSend: function(xhr){
xhr.withCredentials = true;
},
success: function(x, status, xhr){
},
error: function(xhr, status, error){
}
});
Run Code Online (Sandbox Code Playgroud)
PHP提供像这样的CORS请求......
header('Access-Control-Max-Age: 1728000');
header('Access-Control-Allow-Origin: http://someotherdomain.com');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-MD5, X-Alt-Referer');
header('Access-Control-Allow-Credentials: true');
header("Content-Type: application/json; charset=utf-8");
Run Code Online (Sandbox Code Playgroud)
根据所有文档,只要"Access-Control-Allow-Credentials"服务器端头和"withCredentials = true"客户端头设置,域之间的会话cookie处理应该是透明的.我错过了什么吗?