How to make the Browser include the cookies of the domain of the host origin when making a cross-origin HTTP request

。_饼干妹妹 提交于 2019-12-25 01:55:26

问题


I made a cross-origin HTTP request from the website 'demo.home.com' to 'demo.company.com' using the Fetch api with the credentials set to 'include'. There are two cookies. One is 'cookie_home=123; Domain=demo.home.com', the other is 'cookie_company=456; Domain=demo.company.com'. As a result, the cookie 'cookie_company' was included by the request. Is there any way to let the cookie 'cookie_home' be included by the request?

// the request is made in the website 'http://demo.home.com'
// the cookies are:
// 'cookie_home=123; Domain=demo.home.com'
// 'cookie_company=456; Domain=demo.company.com'
fetch('http://demo.company.com/api/test', {
    method: 'GET',
    credentials: 'include'
});

回答1:


You can't. fetch (and XMLHttpRequest) provide no mechanism to manually set cookies in a request.

They'll only send cookies in the browser's cookie jar for the target URL. This applies the normal rules for which domain a cookie belongs to.

You need to use some other mechanism to send the data you would store in the cookies.



来源:https://stackoverflow.com/questions/55372953/how-to-make-the-browser-include-the-cookies-of-the-domain-of-the-host-origin-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!