2016-03-10 154 views
8

我最近從使用jQuery轉向與Redux一起使用同構獲取。當在IE中運行時,它設法罰款。不過,我在Chrome中運行時會看到下面的內容。401(未經授權)在Chrome中,但不在IE中

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

http://stackoverflow.com/questions/29782222/jquery-ajax-call-results-in-401-unauthorized-response-when-in-chrome-or-firefo –

+1

問題尋求調試幫助(「爲什麼isn'這個代碼是否工作?「)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

@CanÜrek我的問題涉及到isomorphic-fetch,jQuery可以很好的跨瀏覽器。 –

回答

24

您對服務器基於Cookie的身份驗證。在這種情況下,它可能與fetchcredentials密鑰相關。 XHR請求,在jQuery的使用總是發送您的cookie,但使用fetch你應該通過credentials選項與

  • same-origin如果你把請求發送到同一個原點(域)
  • include否則

喜歡這個:

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

我認爲它在IE中有效,因爲fetch polyfill在底層使用了XHR請求。

+0

我會給這個鏡頭,但這看起來完全像這個問題。謝謝! –

+1

工作完美! –

相關問題