【问题标题】:Axios PUT Data with Params带有参数的 Axios PUT 数据
【发布时间】:2021-06-03 14:10:01
【问题描述】:

我的后端 API 路由是/api/updateUser/:id 我应该如何将数据发布到这个 API 中?我熟悉非参数 API 的 POST 请求,但这个路由中有一个 /:id

谁能给我看这个演示代码的例子

state = {
    username: "random123",
    password: "random123",
    userid: "qwertyuiop",
  };


saveDetails = async () => {
    const { username, password, userid } = this.state;
    let data = new FormData();
      data.append('username',username);
      data.append('password',password);
    axios
      .put(apiEndPoint+'?id='+this.state.userid, data) //this is where I need help
      .then(async (response) => {
        if (response.data) {
          console.log("success");
        } else {
          console.log("issue");
        }
      })
      .catch((err) => {
        console.log("error",err);
      });
  };

【问题讨论】:

  • 这是一个 path 参数,你为什么要提供它作为 query 参数?只需将其放在 URL 中,`${apiEndPoint}/${this.state.userId}`
  • 谢谢先生!我是一个新手,不知道“路径参数”这个术语。我解决了这个问题,它现在可以工作了。

标签: reactjs react-native post axios expo


【解决方案1】:

这是 Path Parameter Axios PUT 请求的工作示例 -

saveDetails = async () => {
    const { username, password, userid } = this.state;
    axios
      .put(apiEndPoint+"updateUser/"+userid, {
        username:username,
        password:password,
      })
      .then(async (response) => {
        if (response.data) {
          console.log("done");
          
        } else {
          console.log("error");
        }
      })
      .catch((err) => {
        console.log("error",err);
      });
  };

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2011-12-23
    • 2020-10-13
    • 2019-04-25
    • 1970-01-01
    • 2020-10-20
    • 2021-08-12
    • 2018-02-25
    • 2019-04-07
    相关资源
    最近更新 更多