【问题标题】:how to pass parametrs to api?reactjs如何将参数传递给api?react js
【发布时间】:2022-01-06 13:58:06
【问题描述】:

这是我的目标 .....值来自登录(可以是任何东西)-> 例如 电子邮件:'aaaa@gmail.com' 密码:'12345678'

我应该在“@”之前取用户名并将其作为用户名传递给 api。

export const loginUser = (values, history, setFieldError, setSubmitting) => {
    console.log("values from login", values)

    //here i take the email split value and take as user name 
    const username = values.email.split("@")[0]
    console.log("user name", username)

     //now i should pass the username as a name parametr to api

    return () => {
        axios.get('https://api.agify.io/?', values
        ).then((response) => {
            //if res ok should redirect it 
            console.log("response", response)
            // history.push("/user")
        }).catch(error => console.error(error))
        setSubmitting(false);
    }
}

如果它可以帮助这里是我的链接 https://codesandbox.io/s/charming-jang-py0mu?file=/src/auth/actions/userActions.js

【问题讨论】:

  • 如果你想向 API 发送数据,你不应该使用 GET 方法。可能您需要 POST 方法并将凭据作为正文发送。

标签: javascript reactjs api redux query-parameters


【解决方案1】:

我认为这可以完成这项工作(使用模板文字):

axios.get(`https://api.agify.io/?name=${username}`)

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 2015-08-16
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2020-01-09
    • 2021-10-19
    相关资源
    最近更新 更多