Chrome 中的 401(未授权),但 IE 中没有

401 (Unauthorized) in Chrome, but not in IE

我最近从使用 jQuery 转向使用 Redux 的 isomorphic-fetch。当在 IE 中 运行 时,它设法很好地获取。但是,当 运行 in Chrome.

时,我得到以下信息
Failed to load resource: the server responded with a status of 401 (Unauthorized)

值得注意的是,网络 api 确实启用了 windows 身份验证。

这是执行提取的代码:

export const fetchSearchResults = (name) => {
  return (dispatch) => {
    dispatch(requestSearchResults(name))
    return fetch(API URI HERE)
      .then(response => response.json())
      .then(json => {
        console.log('Fetch: ' + json.message.features.length)
        dispatch(receiveSearchResults(json))
      })
  }
}

我想你在服务器上有基于 cookie 的身份验证。在这种情况下,它可能与 fetchcredentials 键相关。在 jQuery 中使用的 XHR 请求始终 发送您的 cookie,但是使用 fetch 您应该通过 credentials 选项和

  • same-origin 如果您向同源(域)发出请求
  • include否则

像这样:

...
fetch(API_URI_HERE, {credentials: 'same-origin'})
...

我假设它在 IE 中工作,因为 fetch polyfill 在后台使用 XHR 请求。