Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 从返回响应数据的Fetch Post获取数据_Reactjs_Fetch - Fatal编程技术网

Reactjs 从返回响应数据的Fetch Post获取数据

Reactjs 从返回响应数据的Fetch Post获取数据,reactjs,fetch,Reactjs,Fetch,我在react应用程序中使用交叉提取,并使用redux。在我的reducer中,我使用cross fetch的post方法将一些数据保存到数据库中。我的调用返回一个响应,其中包含一些我需要在保存后分配给状态的数据。不过,我在解析数据时遇到了问题。下面是响应的样子。如何获取分配给body_init的数据 Response headers : Headers {map: {…}} ok : true status : 200 statusText : "OK" type : "default" ur

我在react应用程序中使用交叉提取,并使用redux。在我的reducer中,我使用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))

}
}问题很简单,我没有对返回的数据调用.json。执行以下操作解决了我的问题:

return fetch('http://localhost:3001/api/updateTransactions',
{
  method: 'post',
  body: JSON.stringify(transUpdate),
  headers:{'Content-Type': 'application/json'}
})
.then(
    response => response.json(), //ADDED >JSON() HERE

    error => console.log('An error occurred.', error)
  )
  .then(res =>

    dispatch(completeTransactionsSave(res))

  )
} }

如何将
dispatch(completeTransactionsSave(res))
更新为
dispatch(completeTransactionsSave(res.\u bodyInit))
Ok。我可以试试,但是什么是res.\u bodyInit?为什么我会得到这样的回应?
return fetch('http://localhost:3001/api/updateTransactions',
{
  method: 'post',
  body: JSON.stringify(transUpdate),
  headers:{'Content-Type': 'application/json'}
})
.then(
    response => response.json(), //ADDED >JSON() HERE

    error => console.log('An error occurred.', error)
  )
  .then(res =>

    dispatch(completeTransactionsSave(res))

  )
} }