Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  154] [ 4]  / answers: 1 / hits: 42304  / 6 Years ago, wed, may 9, 2018, 12:00:00

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




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


Response:




Refused to set unsafe header Cookie



More From » http

 Answers
44

JavaScript cannot set cookie headers explicitly.


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


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


There is no way to change the cookies for a different origin from the current origin although you could set location.href to cause the browser to navigate to a URL on the other origin that will send a set-cookie header and redirect back to your page.




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


[#54477] Saturday, May 5, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;