要解决AJAX/JS表单阻止了cookie的设置的问题,可以通过以下代码示例来实现:
withCredentials
属性为true
,以允许跨域请求携带cookie:var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://example.com/api', true);
// 设置其他请求头和参数
xhr.send();
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'https://example.com');
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
请注意,以上示例中的https://example.com
应替换为实际的域名。