小编Pro*_*roV的帖子

Cors - 此 set-cookie 域属性对于当前主机 url 无效

我有一个节点后端,它应该允许来自我在 localhost:3000 上运行的前端应用程序的跨源请求。因此,我已将 cors 策略限制在我的域中。

import csrf from 'csurf';

app.use(
  cors({
    origin: 'http://localhost:3000',
    credentials: true
  })
);

const csrfProtection = csrf({
  cookie: {
    maxAge: 900,
    domain: 'http://localhost:3000'
  }
})

router.get('/csrfToken', csrfProtection, async (req, res, next) => {
  res.json({ token: req.csrfToken() });
});
Run Code Online (Sandbox Code Playgroud)

当我现在向我的服务器端点(在 localhost:5000 上运行)发出请求时,它向我返回以下错误,即无法设置 cookie。

  fetch('http://localhost:5000/csrfToken', {
      method: 'GET',
      credentials: 'include'
 })
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明

javascript ajax xmlhttprequest node.js

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

ajax ×1

javascript ×1

node.js ×1

xmlhttprequest ×1