使用 AngularJS 使用 Cookie 创建 CORS

Making CORS with AngularJS with Cookie

我正在使用 AngularJS 1.4.7。 我想要做的是使用 JSON 负载 (application/json) 和 Cookie header 发出跨源请求。

我阅读了不同的选项和建议,但我的 cookie 和内容类型仍未设置。

我收到 拒绝设置不安全 header "Cookie" 错误消息。

我尝试将 "withCredentials:true" 添加到配置中,但没有成功。

mod.config(function($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
});
$http({
    withCredentials: true,
    method: POST,
    url: url,
    data: jsondata,
    timeout: deferred.promise,
    headers: {
        'Content-Type': 'application/json',
        'Cookie': 'PHPSESSID=am1miv3koldaqh0ppf9r75hdg1',
    }
})

您不能为 XHR object 手动设置 cookie header。这是一条固定规则(请参阅 addRequestHeader 流程的第 5 步),您无法使用 CORS 更改它。

如果您想在不同的源上设置 cookie,则需要使用 HTTP 响应让该源设置它。