我已经检查了所有关于通过 Header 设置 cookie 的帖子,我总是看到人们推荐这样写:
withCredentials: 真
我正在尝试这样做,但遇到了同样的错误。
$scope.myAppsLoginResponse = JSON.stringify(response.data, null, 3);
var dataC = JSON.parse($scope.myAppsLoginResponse);
$cookies.cookie = 'Somethin here as cookie';
var userId = dataC.user_id;
var thePath = '/thePath';
var theURL = 'https://theUrl';
var cookieRes = $cookies.cookie;
document.cookieMyApp = cookieRes;
var headers2 = {};
headers2 ['Accept-Language'] = 'es-ES';
headers2 ['Cookie'] = document.cookieMyApp;
headers2 ['Authorization'] = 'Basic Z2743ASasdh23Q=';
var param = {}
param ['userId'] = userId;
var req2 = {
method: 'GET',
url: theURL + thePath ,
headers: headers2,
params: param,
xhrFields: {
withCredentials: true
}
}
Run Code Online (Sandbox Code Playgroud)
回复:
拒绝设置不安全的标题“Cookie”
JavaScript 无法显式设置 cookie 标头。
您可以使用document.cookie为当前来源设置 cookie 。
您可以使用,withCredentials: true以便先前设置的 cookie 将与跨源 Ajax 请求一起发送。
尽管您可以设置location.href为使浏览器导航到另一个源上的 URL,该 URL 将发送set-cookie标头并重定向回您的页面,但无法更改与当前源不同源的 cookie 。
由于您正在发出同源请求,因此您只需document.cookie在发出请求之前进行设置。
| 归档时间: |
|
| 查看次数: |
18818 次 |
| 最近记录: |