当我尝试从端口 3000 获取它们时,为什么我的 cookie 和 signedCookies 在 req 中为空?

纪尧姆·马克

当我想从带有 express 的 get 请求中检索会话 cookie 时遇到问题。

我的服务器在端口 8000 上运行,而我的应用程序在端口 3000 上运行。

当我直接访问http://localhost:8000/setcookie和 /getcookie 时,我可以获取和设置 cookie 并且它工作正常。另一方面,当我从http://localhost:3000尝试相同的事情并使用 axios 在端口 8000 上获取或发布请求时,请求中的 cookie 和 signedCookies 为空,我不知道为什么。是不是因为即使我正在侦听并向端口 8000 发送请求,我还是从端口 3000 发送它?

我尝试使用 get 和 post 方法来创建 cookie。它们似乎都不起作用。我有点失落。

我希望我的解释足够清楚,非常感谢!

服务器

const app = express()
const port = process.env.PORT || 8000
app.set("port", port)

const http = require('http').Server(app);

//Body Parser
var urlencodedParser = bodyParser.urlencoded({
    extended: true
});
app.use(urlencodedParser);
app.use(bodyParser.json());

//Définition des CORS
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.use(cookieParser("secret")); 

app.get('/setcookie', function (req, res, next) {
  // Update views
  console.log("connection", req);
  var mail = req.body.email
  res.cookie('b', 'jefaisuntest', {signed: true, expire : 600000});

  // Write response
  res.send("cookie sent")
})

app.get('/getcookie', function(req, res) {
    var username = req.signedCookies['b'];

    console.log("iciiii", req)
    if (username) {
        return res.send(username);
    }

    return res.send('No cookie found');
});

const server = http.listen(port, function () {
    console.log(`listening on *:${port}`)
})

客户

import axios from 'axios';

const headers = {
    'Content-Type': 'application/json'
}
const burl = "http://localhost:8000"

export default {
    login : function(email:string, password:string) {
        axios.get(burl + '/setcookie')
    },
    isAuth : function() {
        axios.get(burl + '/getcookie')
    },
}
安德鲁·奥尔特

@mnesarco 说的是正确的 - 从技术上讲,您正在运行两个不同的服务器,这意味着您在两者之间发送的任何请求都是 CORS 请求。为了在 CORS 请求中共享发送 cookie,您需要将该withCredentials标志设置为 true:

export default {
    login : function(email:string, password:string) {
        axios.get(burl + '/setcookie', { withCredentials: true })
    },
    isAuth : function() {
        axios.get(burl + '/getcookie', { withCredentials: true })
    },
}

从类似情况或此 mdn 页面中查看此答案获取更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我运行“React”时,它说端口 3000 正在使用中

为什么当我尝试在 react.js 中获取图像的高度和宽度时返回 undefined

当我尝试将端口3000用于HTTP并将端口443用于HTTPS时,它显示错误错误:绑定EACCES

使用passport-jwt和结尾词,我的req.user为空

为什么当我使用where /和筛选器强制转换getdate()时查询为空

当我尝试访问URL时,Rails日志在Puma的终端中未显示任何内容:localhost:3000

当我尝试打印封面数组时,为什么我的输出为空?

当我尝试将对象推送到MongoDB和Nodejs中的数组时,为什么会出现castError?

为什么当我在带有json数组的PHP中设置cookie时,是否显示%sign?

为什么ZeroMQ req-rep在C ++和Python之间给我一个空响应?

http:// localhost:3000 /#!/为什么我在localhost链接中得到“#!/”。

req.cookies 为空,我不知道为什么

为什么我的req.body在POST上总是为空?

为什么我的 req.body 是空的?MEAN 堆栈 - NodeJs

当我在 ASP.NET 中结合承载令牌和 Cookie 身份验证时,我得到 401

当我从检查器中将Cookie设置为Chrome时,为什么Chrome将其着色为红色?

Cookie在那里,但我无法在CodeIgniter中获得它们。为什么?

当我尝试在 Room 中插入元素时,为什么会出现“空对象引用”异常?

为什么我需要使用 waitForAngular(false) 和 browser.sleep(3000)?

当我尝试通知某事时为空错误

当我使用axios进行发布时,req.body为空,但是当我使用“请求”时,它工作正常

为什么我的Rails会话和cookie无法持久保存?

为什么当我使用@autowired服务初始化对象实例字段时它们仍然为空?

当我尝试显示SD卡中的图像时imageview为空

当我尝试使用MS AJAX PageMethods传递对象时,为什么我的参数为null

为什么我的Cookie在C#ASP.NET空Web项目中显示为null?

当我尝试计算表中的行数时,为什么我的PHP无法正常工作?

当我尝试访问我的类中的属性时,为什么会出现 NameError?

当我尝试在React中映射和渲染数据时应用崩溃