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

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

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

  1. document.cookie = "CookieName=token_value"
  2. document.querySelector("button").addEventListener("click", async () => {
  3. const url = "http://localhost:3000/api/v1/cart";
  4. const data = {
  5. "fingerprint": "de4b27d8beca3167f9ec694d76aa5a35",
  6. "products": [
  7. {
  8. "productID": 573901,
  9. "quantity": "2"
  10. },
  11. {
  12. "productID": 573901,
  13. "quantity": "2"
  14. },
  15. {
  16. "productID": 573901,
  17. "quantity": "2"
  18. }
  19. ]
  20. }
  21. await fetch(url, {
  22. method: 'POST',
  23. credentials: 'include',
  24. headers: {
  25. 'Content-Type': 'application/json',
  26. "referrer":"localHost"
  27. },
  28. body: JSON.stringify(data)})
  29. .then(response => response.json())
  30. .then(data => console.log(data))
  31. .catch(err => console.log(err))
  32. })

appjs

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

暂无答案!

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

相关问题