首页 > 解决方案 > 登录并传递电子邮件地址后,如何使用字符串参数调用 GET api,以便数据出来?(反应.js)

问题描述

我需要在登录后调用带有字符串参数的 GET api,因此将显示用户详细信息。我做的api是带有电子邮件字符串参数的GET API。

api结果如下图: 在此处输入图像描述

我需要使用电子邮件的路径变量调用 api 以获取详细信息。

class Home extends Component {

constructor(props){
  super(props);
  this.state ={
    loginEmail: this.props.location.state.loginEmail,
    isLoaded: false,
    redirect: false
  }
  this.logout = this.logout.bind(this);
}

componentDidMount(){
  fetch('https://ems-unimas-58134.herokuapp.com/api/users/view/${email}')
  .then(res => res.json())
  .then(json => {
    this.state({
      isLoaded: true,

    })
  });
}

我不确定这是否是我在 componentDidMount 中的正确方式。顺便问一下,有没有其他方法可以在我登录主页后自动调用api。

标签: reactjsapipostfetch

解决方案


其他方式可以是……您可以在登录 api 成功响应后调用此 api。我是说

fetch('url to login').then(res => res.json())
  .then(json => {
fetch('https://ems-unimas-58134.herokuapp.com/api/users/view/${email}')....
    )

推荐阅读