基于条件的 API 调用 [英] API Calls based on Condition

查看:22
本文介绍了基于条件的 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dropdown with 2 values (value 1 and value 2) and i have 2 API POST calls . i have one button. what i want is is the value one is selected perform the POST call for value 1 if value 2 is selected perform API post for value 2 and show their respective results. please guide me how to achieve this

我的 POST 电话:

MY POST Call:

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

In this Post call the "Model:this.state.model" is the selected dropdown value. i need to call the api based on selected dropdown value

推荐答案

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

I hope this code will solve your issue;

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

这篇关于基于条件的 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆