最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 401 (Unauthorized) in Chrome, but not in IE - Stack Overflow

programmeradmin9浏览0评论

I recently moved from using jQuery to using isomorphic-fetch with Redux. When running in IE, it manages to fetch fine. However I get the below when running in Chrome.

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

It may be worth noting that the web api does have windows authentication enabled.

Here's the code that executes the fetch:

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))
      })
  }
}

I recently moved from using jQuery to using isomorphic-fetch with Redux. When running in IE, it manages to fetch fine. However I get the below when running in Chrome.

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

It may be worth noting that the web api does have windows authentication enabled.

Here's the code that executes the fetch:

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))
      })
  }
}
Share Improve this question edited Mar 10, 2016 at 9:42 Jacob Mason asked Mar 10, 2016 at 9:23 Jacob MasonJacob Mason 1,4055 gold badges15 silver badges29 bronze badges 4
  • stackoverflow./questions/29782222/… – Can Ürek Commented Mar 10, 2016 at 9:25
  • 1 Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Marcos Pérez Gude Commented Mar 10, 2016 at 9:25
  • @CanÜrek My question relates to isomorphic-fetch, jQuery works fine cross-browser. – Jacob Mason Commented Mar 10, 2016 at 9:45
  • This question feels well-worded to me, FWIW. And the accepted answer appears to work, for the reasons the answerer describes. – ericsoco Commented Nov 4, 2016 at 20:49
Add a ment  | 

1 Answer 1

Reset to default 28

I suppose you have cookie-based authentication on server. In this case it could be related to credentials key for fetch. XHR requests, that used in jQuery always send your cookie, but using fetch you should pass credentials option with

  • same-origin if you make request to the same origin (domain)
  • include otherwise

Like this:

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

I assume that it works in IE because fetch polyfill uses XHR requests under the hood.

发布评论

评论列表(0)

  1. 暂无评论