使用XMLHttpRequest2设置Cookie

我试图设置一个cookie使用XMLHttpRequest。 我在XHR发布请求的响应中看到一个“Set-Cookie”头文件,但在document.cookie中看不到cookie。 这很好,但是我最终希望cookies不会暴露在JavaScript环境中,但是我没有看到任何后续的脚本请求,所以我认为cookie会自动附加到浏览器的请求中。 这是不正确的? 我对cookie是否被设置有点困惑,我可以用一些帮助来解决这个问题。

以下是客户端代码的样子:

(function() { var xhr = new XMLHttpRequest(); var authUrl= "http://api.myserver.io/cookie" xhr.addEventListener("load", reqListener); xhr.open("POST", authUrl, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.credentials = true; xhr.send(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { debugger; // I'm not seeing anything in xhr.request } } var reqListener = function() { debugger; } })() 

服务器代码正在使用expreess并正在使用此function处理请求:

 var setCookie = function(req, res) { res.cookie('test-cookie', Date.now(), { maxAge: 3600000000, path: '/' }); res.status(200).end() } 

我需要设置xhr.withCredentials = true ,而不是xhr.credentials = true