【问题标题】:API Calls based on Condition基于条件的 API 调用
【发布时间】:2021-12-16 18:45:37
【问题描述】:

我有一个包含 2 个值(值 1 和值 2)的下拉列表,并且我有 2 个 API POST 调用。我有一个按钮。我想要的是选择值 1 如果选择了值 2,则对值 1 执行 POST 调用 对值 2 执行 API 发布并显示它们各自的结果。请指导我如何实现这一目标

我的 POST 电话:

submit(){
      const article = {  model: this.state.model,
        dataset:this.state.course,
        path:this.state.checkbox };

      axios.post('http://localhost:3000/home', article)
        .then(response => this.setState({ s3path: response.data.s3_path , triton_output: response.data.triton_output, confidence: response.data.confidence,
          inference_model:response.data.inference_model, loading: false,
        
        }))
        .catch(error => {
            this.setState({ errorMessage: error.message });
            console.error('There was an error!', error);
        });
        
       this.openModal();
       this.state.loading=true
    }
    submit1(){
      const article = {  model: this.state.model,
        dataset:this.state.course,
        path:this.state.checkbox };

      axios.post('http://localhost:3000/home', article)
        .then(response => this.setState({ x: response.data.x , y: response.data.y_output, height: response.data.height,
          inference_model:response.data.inference_model, loading: false,
        
        }))
        .catch(error => {
            this.setState({ errorMessage: error.message });
            console.error('There was an error!', error);
        });
        
       this.openModal();
       this.state.loading=true
    }

在这篇文章中,“Model:this.state.model”是选定的下拉值。我需要根据选定的下拉值调用 api

【问题讨论】:

    标签: reactjs api axios fetch dropdown


    【解决方案1】:

    我希望这段代码能解决你的问题;

        var Test = React.createClass({
       onClick: function(event){
        if("Dropdown value check")
        {
            func1();
        }
        else{
            func2();
        }
    
       },
       render: function(){
          return (
             <a href="#" onClick={this.onClick}>Test Link</a>
          );
       }
    });
    

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多