首页 > 解决方案 > 从 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 。

标签: javascriptreactjs

解决方案


试试 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,告诉我,我会帮助你


推荐阅读