V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ethusdt
0.01D
V2EX  ›  问与答

fetch api 在浏览器内无法保存和发送 cookie?

  •  
  •   ethusdt · Dec 24, 2018 · 2418 views
    This topic created in 2687 days ago, the information mentioned may be changed or developed.

    应该是使用姿势有问题, 特来请教.

    网上的答案有:

    1. 需要设置 credentials: 'include' ✅
    2. 跨域需要设置 Access-Control-Allow-Credentials: true ✅
    3. 跨域需要设置 Access-Control-Allow-Origin: http://localhost:3000 ✅

    这三个都设置的正常:

    fetch(COR_API, {
      credentials: 'include',
      method: 'POST',
      body: ...
      headers: {'Accept': 'application/json'}
    })
    

    chrome

    在 chrome-devtool 中没有 Set-Cookie 字段.

    在 Safari 和 Firefox 中会有 Set-Cookie 字段, 但是不会保存 cookie.

    使用 postman 测试, 发现是正常的, 第二次请求会带上第一次的 cookies.

    postman

    SO 上的提问

    4 replies    2018-12-24 20:35:49 +08:00
    ethusdt
        1
    ethusdt  
    OP
       Dec 24, 2018
    附上 Safari 的 response

    ethusdt
        2
    ethusdt  
    OP
       Dec 24, 2018
    使用 axios 也是一样的结果..
    ethusdt
        3
    ethusdt  
    OP
       Dec 24, 2018
    哈哈哈哈 😂

    结果是 server 把 cookie 的过期时间设置错了, 服务器时间和本地时间差 8 小时.. i18n

    No one can save my day..
    ethusdt
        4
    ethusdt  
    OP
       Dec 24, 2018
    哦.. 不是三楼的答案.. 而是 因为浏览器禁止了第三方 cookie, 需要手动打开. chrome 和 Safari 是这样的.

    而且在手机上也是禁止第三方 cookie..

    没啥好办法么? 那 h5 页面咋办呢? 只能改成 token 验证吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2377 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:55 · PVG 18:55 · LAX 03:55 · JFK 06:55
    ♥ Do have faith in what you're doing.