我无法使用 axios 从我的 post 请求中获取我的 httpOnly cookie

I can't get my httpOnly cookie from my post request using axios

我遇到了无法从 node.js 后端获取 cookie 的问题。下面是我当前的代码,当我 console.log(req.cookies) 它 returns [Object: null prototype] {}。我过去遇到过类似的问题,我解决的方法只是添加“withCredentials: true”,但由于我已经在我的 axios post 请求中有了它,所以我认为这不是问题所在。

前端

  const save = async () => {
    if (loggedInCookie) {
        await axios.post('http://localhost:5000/savelisting', {
            withCredentials: true,
            link: link,
            car: car,
            price: price,
            picture: picture,
            timeleft: timeleft,
            site: site,
            milage: milage,
            location: location,
            trans: trans
        });

    } else {
        console.log("please login to save this listing")
    };

};

后端

app.post('/savelisting', async (req, res) => {
    console.log(req.cookies);

    try {
    var jwtToken = await req.cookies.AccessToken;
    console.log(jwtToken);

    // Grooms cookie
    jwtToken = jwtToken
                    .split('; ')
                    .find(row => row.startsWith('AccessToken='))
                    .split('=')[1];


    const decoded = jwt.verify(jwtToken, process.env.TOKEN_KEY);
    var userId = decoded.id
    console.log(userId) 
 
    const link = req.body.link;
    const car = req.body.car;
    const price = req.body.price;
    const picture = req.body.picture;
    const timeleft = req.body.timeleft;
    const site = req.body.site;
    const milage = req.body.milage;
    const location = req.body.location;
    const trans = req.body.trans;

    } catch(err) {
        console.log(err)
    };
    res.status(200).send();
});

您正在将 withCredentials 设置作为数据的一部分发送到您的 API。它应该作为一个选项传递给 axios:

axios.post(
    'http://localhost:5000/savelisting',
    {    
        link: link,
        car: car,
        ...
    },
    {
        withCredentials: true
    }
)