我正在为一个简单的react / redux应用程序建模auth层。在服务器端,我有一个基于devise_token_auth gem 的API 。
我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: 'test@test.it', password: 'whatever' });
Run Code Online (Sandbox Code Playgroud)
这行得通,我得到200响应以及所需的所有数据。我的问题是,信息在响应正文和标头之间划分。
我可以这样解析JSON主体:
postLogin({ 'test@test.it', password: 'whatever' })
.then(res => res.json())
.then(resJson => dispatch(myAction(resJson))
Run Code Online (Sandbox Code Playgroud)
但是,这样myAction就不会从标头中获取任何数据(在解析JSON时丢失)。
有没有办法从fetch请求中获取标头和正文?谢谢!