Reading response headers with Fetch API

后端 未结 6 1631
Happy的楠姐
Happy的楠姐 2020-11-28 06:42

I\'m in a Google Chrome extension with permissions for \"*://*/*\" and I\'m trying to make the switch from XMLHttpRequest to the Fetch API.

The extensio

6条回答
  •  生来不讨喜
    2020-11-28 07:11

    Also remember you may need to pass the response to next .then() after res.headers.get() if you parse it. I keep forgetting about that...

    var link
    const loop = () => {
      fetch(options)
        .then(res => { 
          link = res.headers.get('link')
          return res.json()
        })
        .then(body => {
          for (let e of body.stuff) console.log(e)
          if (link) setTimeout(loop, 100)
        })
        .catch(e => {
          throw Error(e)
        })
    }
    loop()
    

提交回复
热议问题