Cookie和cors,fetch和socketio-在使用套接字和本地主机服务器时,如何在RESTAPI的fetch请求中包含Cookie

jmp7cifd  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(155)

我目前是网络开发的新起点,所以没有最好的经验,但我遇到了一个反复出现的问题,我无法解决,我希望有人能帮助我。
我正在向我的api发出获取后请求,由于某种原因,我在clientjs文件中定义的cookie没有被包括在内。我有:
将凭据设置为“包括”在提取请求中,
在clientsidejs文件的顶部声明cookie,
在我的服务器端js文件中将cors origin设置为我的本地服务器的特定url,
不知怎的,饼干还没有包括在内。最让人恼火的是,它在postman上运行得非常完美,所以我知道除了这个获取请求之外,问题并不是别的。
clientjs

document.cookie = "CookieName=token_value"
document.querySelector("button").addEventListener("click", async () => {

    const url = "http://localhost:3000/api/v1/cart";
    const data = {
        "fingerprint": "de4b27d8beca3167f9ec694d76aa5a35",
        "products": [
            {
                "productID": 573901,
                "quantity": "2"
            },
            {
                "productID": 573901,
                "quantity": "2"
            },
            {
                "productID": 573901,
                "quantity": "2"
            }
        ]
    }

    await fetch(url, {
        method: 'POST',
        credentials: 'include',
        headers: {
            'Content-Type': 'application/json',
            "referrer":"localHost"
            },
        body: JSON.stringify(data)})
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(err => console.log(err))

})

appjs

const server = require('http').createServer(app);
global.io = require('socket.io')(server,{
    cors:{
        origin:'http://localhost:5501',
        methods:["GET","POST"],
        credentials:true,
    }
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题