首页 > 解决方案 > 基于条件的 API 调用

问题描述

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

我的帖子电话:

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

标签: reactjsapiaxiosfetchdropdown

解决方案


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

    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>
      );
   }
});

推荐阅读