我正在爲簡單的react/redux應用程序對auth層進行建模。在服務器端,我有一個基於devise_token_auth寶石的API。從提取中提取JSON和頭文件()
我使用fetch
在要求張貼標誌:
const JSON_HEADERS = new Headers({
'Content-Type': 'application/json'
});
export const postLogin = ({ email, password }) => fetch(
`${API_ROOT}/v1/auth/sign_in`, {
method: 'POST',
headers: JSON_HEADERS,
body: JSON.stringify({ email, password })
});
// postLogin({ email: '[email protected]', password: 'whatever' });
這工作,我也得到一個200響應,所有我需要的數據。我的問題是,信息被分爲響應正文和標題。
- 主體:用戶信息
- 頭:訪問令牌,過期等
我可以解析JSON體是這樣的:
postLogin({ '[email protected]', password: 'whatever' })
.then(res => res.json())
.then(resJson => dispatch(myAction(resJson))
但隨後myAction
不會從頭文件中獲取任何數據(解析JSON時丟失)。
有沒有辦法從fetch
請求中獲取標題和正文? 謝謝!
嗨,謝謝你的回答。不幸的是,自從res.json()返回一個參數以來,'myAction'需要承諾作爲參數。我們希望將簡單的數據傳遞給非thunk動作創建者。 – nerfologist