javascript - 从 redux store 获取 API 响应并发送响应
问题描述
我想要执行的是发布请求并访问 res.json() 并获取 res.json() 我想调度一个操作来更新存储并访问组件中的响应对象。一个小的代码示例会很棒.
export const filesDownload = (postData) => dispatch =>{
console.log(postData);
fetch('http://'+ip+':8000/api/v1/integrator/email/need_attachment/',{
method:'POST',
headers: {
'content-type': 'application/json'
},
body : JSON.stringify(postData)
})
.then(res => console.log(res.json()))
.then(files => dispatch({
type: GET_FILES,
payload:files
}))
}
我想将 res.json() 发送到 store 。
解决方案
试试 axios 它的清洁器:
export const filesDownload = (postData) => dispatch =>{
console.log(postData);
axios.post('http://'+ip+':8000/api/v1/integrator/email/need_attachment/')
.then((response)=>response.data)
.then((files)=>dispatch({
type: GET_FILES,
payload:files
}))
.catch((err)=>console.log(err))
}
如果它不起作用,你将不得不使用 redux Thunk,告诉我,我会帮助你
推荐阅读
- python - 有没有办法将 main.py 文件放入文件夹中?
- excel - 具有多种复杂性的 IF 函数
- r - 如何在R中按组计算某些变量的比例?
- ruby - Ruby Terminal -e:根据 $_ 比较做出决定
- powershell - 在内联 PowerShell 脚本中访问 YAML 变量
- javascript - 动态创建的输入文件字段在 change() 调用时未在画布中显示图像
- tkinter - 在 Tkinter 窗口的标题中添加徽标
- docker - 在 NGINX Docker 上托管多个 Web 应用程序
- java - 为什么反应流订阅者在当前线程中执行?
- r - 通过并行处理了解光栅的 writeValues。是否可以在使用 mclapply fork 集群时为每个栅格写入值。R