我在带有 redux 的 React 应用程序中使用交叉获取。在我的减速器中,我使用 cross fetch 的 post 方法将一些数据保存到数据库中。我的调用返回一个响应,其中包含一些我需要在保存后分配给状态的数据。但我在解析数据时遇到了麻烦。下面是响应的样子。如何获取分配给 body_init 的数据?
Response
headers
:
Headers {map: {…}}
ok
:
true
status
:
200
statusText
:
"OK"
type
:
"default"
url
:
"http://localhost:3001/api/updateTransactions"
_bodyInit
:
"[{"category":"Dining Out","months":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}},{"category":"Auto","months":
:
:
:
这是完整的通话。我的困惑是,从api返回的数据实际上已经格式化为json(api返回:res.json(items))
export function saveTransactions(transUpdate,year) {
return function (dispatch) {
dispatch(requestTransactionsSave(transUpdate))
return fetch('http://localhost:3001/api/updateTransactions',
{
method: 'post',
body: JSON.stringify(transUpdate),
headers:{'Content-Type': 'application/json'}
})
.then(
response => response,
error => console.log('An error occurred.', error)
)
.then(res =>
dispatch(completeTransactionsSave(res))
)
}
}