javascript - 401(未经授权)在 Chrome 中,但在 IE 中没有

标签 javascript google-chrome fetch redux unauthorized

我最近从使用 jQuery 转向使用 Redux 的 isomorphic-fetch。在 IE 中运行时,它可以正常获取。但是,在 Chrome 中运行时,我得到以下信息。

Failed to load resource: the server responded with a status of 401 (Unauthorized)

可能值得注意的是,web 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 key 相关。在 jQuery 中使用的 XHR 请求总是发送你的 cookie,但是使用 fetch 你应该传递 credentials 选项和

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

像这样:

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

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

关于javascript - 401(未经授权)在 Chrome 中,但在 IE 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912271/

相关文章:

javascript:读取html标签中的lang属性

javascript - 如何使用 rollup 和 commonjs 插件部分包含 Node.js 模块

javascript - 如何设置 d3 符号的大小?

react-native - FormData 在 react native 中发送字符串值而不是视频文件

javascript - Backbone.js 集合获取 'this._byId' 未定义

javascript - 防止在 meteor 中单击表单按钮时重新加载页面?

html - 如何更改可调整大小的文本区域的光标?

html - Chrome- 显示 :none options in select

javascript - ReactJS - 获取函数: Why am I getting uncaught (in promise) SyntaxError: Unexpected end of JSON input?

javascript - Google Chrome 即时编辑 Javascript