【问题标题】:Calling API using axios in react native getting 404 error when using params在使用参数时使用 axios 在反应本机中调用 API 得到 404 错误
【发布时间】:2021-03-10 07:09:06
【问题描述】:

我正在使用 axios 调用 api。下面的代码工作正常

    <ButtonComponent
    label="SIGN IN"
    onPress={() => {
         axios
           .get(
             "http://example.com/Api/Users?email=abc@gmail.com&password=abc"
           )
        .then((res) => {
          console.warn(res.data);
        })
        .catch((error) => {
          console.warn(error);
        });
    }}
  />

但是当我用参数而不是完整的字符串发送它时,它会给出错误“请求失败,状态为 404”

    <ButtonComponent
    label="SIGN IN"
    onPress={() => {
      axios
        .get("http://example.com/Api/Users?", {
          params: { email: { email }, password: { password } },
        })
        .then((res) => {
          console.warn(res.data);
        })
        .catch((error) => {
          console.warn(error);
        });
    }}
  /> 

请指导我做错了什么?

【问题讨论】:

  • 这个params: { email: { email }, password: { password } }应该是params: { email: email, password: password }还是只是params: { email, password }

标签: react-native axios


【解决方案1】:

您不需要为参数值加上花括号。

axios.get("http://example.com/Api/Users?", {
          params: { 
                    email: abc@gmail.com,
                    password: abc 
                  },
        })

【讨论】:

    【解决方案2】:

    试试这个方法

    axios.get('/api', {
      params: {
        email: 'email',
        password: 'password'
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多