标头javascript中的Cookie错误“拒绝设置不安全的标头” Cookie”。 [英] Cookie in header javascript error "Refused to set unsafe header "Cookie""

查看:247
本文介绍了标头javascript中的Cookie错误“拒绝设置不安全的标头” Cookie”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了所有有关通过Header设置cookie的帖子,并且我总是看到人们建议这样做:

I have check all the post about setting the cookie via Header, and I always see that the people recommend put this:


withCredentials :true

withCredentials: true

我正在尝试执行该操作,并遇到了相同的错误。

I'm trying to do it and got the same error.

 $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
             }
        }

响应:


拒绝设置不安全的标头 Cookie

Refused to set unsafe header "Cookie"


推荐答案

JavaScript无法显式设置Cookie标头。

JavaScript cannot set cookie headers explicitly.

您可以使用 document.cookie 当前来源设置cookie。

You can use document.cookie to set a cookie for the current origin.

您可以使用 withCredentials:true ,以便先前设置的cookie会与跨域Ajax请求一起发送。

You can use withCredentials: true so that previously set cookies will be sent with a cross-origin Ajax request.

无法将Cookie更改为与当前来源不同的来源。

There is no way to change the cookies a different origin from the current origin.

发出相同的原始请求之前,需要先设置 document.cookie

Since you are making a same origin request all you need to do is set document.cookie before you make the request.

这篇关于标头javascript中的Cookie错误“拒绝设置不安全的标头” Cookie”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆