我最近从使用 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 的身份验证。在这种情况下,它可能与 fetch
的 credentials
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/