【发布时间】:2012-11-07 14:37:25
【问题描述】:
我意识到这个问题已经被问了十几次,每个回答都表明我做得对,但也许我错过了一些东西。
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){
}
});
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");
根据所有文档,只要设置了“Access-Control-Allow-Credentials”服务器端标头和“withCredentials=true”客户端标头,域之间的会话 cookie 处理应该是透明的。我错过了什么吗?
【问题讨论】:
-
Answer :) - 您在问题中缺少浏览器名称和 jQuery 版本...另请查看 stackoverflow.com/questions/2054316/…
-
jQuery v1.8.3 & Mozilla Firefox & Google Chrome 我实际上是在设置那个标志,正如你在上面的代码中看到的那样