使用Cookie使用AngularJS制作CORS

时间:2015-10-15 21:44:11

标签: javascript json angularjs cookies cors

我使用的是AngularJS 1.4.7。 我想要做的是使用带有Cookie标头的JSON有效负载(application / json)发出交叉原始请求。

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

我正在拒绝设置不安全的标头" 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',
    }
})

1 个答案:

答案 0 :(得分:1)

您无法手动为XHR对象设置Cookie标头。这是一个坚定的规则(参见addRequestHeader流程的第5步),您无法使用CORS进行更改。

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