我有一个成功预飞行的跨域 AJAX GET,但 cookie 没有附加到 GET 请求。当用户单击登录按钮时,会进行 POST 以使用户登录,这可以跨域正常工作。JavaScript 是:
$.ajax(signin_url, {
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(credentials),
success: function(data, status, xhr) {
signInSuccess();
},
error: function(xhr, status, error) {
signInFailure();
},
beforeSend: function(xhr) {
xhr.withCredentials = true
}
});
响应标头包括一个 cookie:
Set-Cookie:user_token=snippysnipsnip; path=/; expires=Wed, 14-Jan-2032 16:16:49 GMT
如果登录成功,将发出 JavaScript GET 请求以获取当前用户的详细信息:
function signInSuccess() {
$.ajax(current_user_url, {
type: "GET",
contentType: "application/json; charset=utf-8",
success: function(data, status, xhr) {
displayWelcomeMessage();
},
beforeSend: function(xhr) {
xhr.withCredentials = true;
}
});
}
Chrome 的 OPTIONS 请求返回的与 CORS 相关的标头是:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With, X-Prototype-Version, Content-Type, Origin, Allow
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin:http://192.168.0.5
Access-Control-Max-Age:1728000
但是,不会在 GET 请求上发送任何 cookie。