小编Avi*_*ash的帖子

如何将 fetch 转换为 axios

我有以下代码段,运行完美。然而,我的任务是用 axios 替换 fetch。您能指导一下,axios 中正确的代码替换是什么吗?

const create = async (credentials, software) => {
  return await fetch('/api/software/create', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + credentials.t
    },
    credentials: 'include',
    body: JSON.stringify(software)
  })
    .then((response) => {
      return response.json()
    }).catch((err) => console.log(err))
}

create({ t: jwt.token }, data)
    .then((data) => {
      if (data.error) {
        this.setState({ error: data.error })
      } else {
        this.props.dispatch(initSoftware()); //if successful get the list of softwares in redux store
      }
    })
Run Code Online (Sandbox Code Playgroud)

data 变量是一个包含 req.body 等价物的对象...上面的代码是用 …

node.js reactjs

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

node.js ×1

reactjs ×1