【问题标题】:how to set data in DataTable from Axios getting response from multiple API and with Promise.all如何从 Axios 获取来自多个 API 的响应并使用 Promise.all 在 DataTable 中设置数据
【发布时间】:2021-08-27 20:12:17
【问题描述】:

我创建了带有 switch case 的代码,以通过按标签来调用 API。然后我有一个使用 Axios 的带有参数的帖子 URL。我也使用promise。检查以下代码。

$('#myTab button').on('shown.bs.tab', function (e) {
var tab = $(this).attr("id");
  // console.log(tab);
            
        switchtabs(tab);
        return true;
      }else{
        alert("Please select Domain Name");
        return false
      }
});
function switchtabs(tab){
          var url = '';let prm;
    switch (tab) {
        case 'second-tab':
            url = link1;
            prm = {type:'sample1', fromdate:fromdate, todate:todate, descn:info}
            console.log(url, prm);
            break;
        case 'third-tab':
            url = link2;
            prm = {type:'sample1', fromdate:fromdate, todate:todate, descn:info}
            console.log(url, prm);
            break;
        case 'fourth-tab':
            url = link3;
            prm = {type:'sample1', fromdate:fromdate, todate:todate, descn:info}
            console.log(url, prm);
            break;
        default: 
            url = link;
            prm = {type:'domainwise', fromdate:fromdate, todate:todate, descn:shop}
            console.log(url, prm);
    }
    if(typeof url !== "undefined"){
      // let prm = {type:'domainwise', fromdate:fromdate, todate:todate, descn:'1'}

 var main = axios.post(url, prm).then(function(response){
console.log(response.data)
})

Promise.all([main]).then(function(values){
 return values
 }).catch(function(err){
  console.log(err);
 })


    }
 }

选择标签时,响应数据发生。但是如何在 dataTable 中添加与 tab-pane 相关的数据。有多个具有不同 IDs 和不同 标题 的数据表。我试图通过函数 DataTable() 添加。提前致谢。

【问题讨论】:

    标签: javascript api axios rest


    【解决方案1】:

    我在 prm 下创建了一个标签。像标签=“演示”。然后当 URL 和 param 传入 Axios 时。之后我添加了 if 条件标签 == "demo"。我的代码正在运行。

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2021-12-02
      • 2021-09-05
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多