Javascript 我需要用不同的主体类型执行一个API

Javascript 我需要用不同的主体类型执行一个API,javascript,reactjs,Javascript,Reactjs,我的任务是我需要以两种不同的方式调用此API,这意味着如果在我单击提交时出现第一个屏幕,则此API将执行finalCallForInitiate1否则finalCallForInitiate2此将执行需要在finalCallForInitiate2()之后设置这两个API;这个API让你可以看到onClick下面的代码,两个API不能同时执行,需要根据屏幕设置 不同主体的首个API const finalCallForInitiate1 = () => { let body;

我的任务是我需要以两种不同的方式调用此API,这意味着如果在我单击提交时出现第一个屏幕,则此API将执行finalCallForInitiate1否则finalCallForInitiate2此将执行需要在finalCallForInitiate2()之后设置这两个API;这个API让你可以看到onClick下面的代码,两个API不能同时执行,需要根据屏幕设置

不同主体的首个API

  const finalCallForInitiate1 = () => {
    let body;

    body = {
      screening : "single Screening",
      timestamp: props.ts,
    };

    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
    axios
      .post(`${DJANGO_SERVER_ADDRESS}/data/initiate/`, body, config)
      .then(
        (res) => {
          console.log(".......................", res);
          
        },
        (err) => {
          
        }
      );
      
  };   
const finalCallForInitiate2 = () => {
    let body;

    body = {
      screening : "dual Screening",
      timestamp: props.ts,
    };

    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
    axios
      .post(`${DJANGO_SERVER_ADDRESS}/data/initiate/`, body, config)
      .then(
        (res) => {
          console.log(".......................", res);
          
        },
        (err) => {
          
        }
      );
      
  };
相同的API具有不同的主体

  const finalCallForInitiate1 = () => {
    let body;

    body = {
      screening : "single Screening",
      timestamp: props.ts,
    };

    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
    axios
      .post(`${DJANGO_SERVER_ADDRESS}/data/initiate/`, body, config)
      .then(
        (res) => {
          console.log(".......................", res);
          
        },
        (err) => {
          
        }
      );
      
  };   
const finalCallForInitiate2 = () => {
    let body;

    body = {
      screening : "dual Screening",
      timestamp: props.ts,
    };

    const config = {
      headers: {
        "Content-Type": "application/json",
      },
    };
    axios
      .post(`${DJANGO_SERVER_ADDRESS}/data/initiate/`, body, config)
      .then(
        (res) => {
          console.log(".......................", res);
          
        },
        (err) => {
          
        }
      );
      
  };

若您的主体数据只是在更改,那个么创建单个函数并将更改后的主体数据作为参数传递

在上面的例子中,你可以这样做

const finalCallForInitiate1=(筛选)=>{
让身体;
正文={
筛选,
时间戳:props.ts,
};
常量配置={
标题:{
“内容类型”:“应用程序/json”,
},
};
axios
.post(`${DJANGO_服务器地址}/data/initiate/`,正文,配置)
.那么(
(res)=>{
控制台日志(“…………”,res);
},
(错误)=>{
}
);

}; 你解释需求的方式似乎有点夸张。但据我所知,你有一个用

德扬戈

并希望继续处理数据。

“职位”

使用的方法表示您正在转发数据。您还希望继续进行条件渲染。为此,您可能需要检查API体系结构,并检查为fetch()函数提供的
相同API URL
是否能够可靠地处理两种不同数据类型的数据。所有这些都要考虑在内

问题更多的是关于API体系结构


也许你需要给我们更多的细节。我本想发表评论,但我没有足够的基于stackoverflow的声誉发表评论,因此,我将此作为一个指南发布。

我尝试了这种方法,但两个API get execute都需要设置如果屏幕为单屏幕,则只有一个API get execute如果为双屏幕,则双屏幕API将执行excute@dev是的,这就是我要回答的,但你要明白。太好了,谢谢,我会照你的建议处理的