javascript - getJson之后如何携带数据

标签 javascript jquery json getjson

我在 getJson 之后在变量数据中携带数据时遇到问题..下面是我的代码

function format ( d ) {
  var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
   var rows = ''; 
    $.getJSON($base+'id=' + id,function(data){
        var index =0;
        var newOptions = data.data;
        var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
        $.each(SponsorbyFee, function(value,key) {
          // console.log(key);
          switch(key['fee_type_id']){
            case '1' :
                  // console.log('program');
                  break;
            case '2' :
                  // console.log(key['OthersFee']);
                  rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
                  index++;
                  break;
            case '3' : 
                  // console.log(key['DiscountFee']);
                  rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
                  index++;
                  break;
          }
        });
    });
    return '<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
                    '<thead>'+
                          '<tr>'+
                              '<th width="30%">Payment Description</th>'+
                              '<th width="10%">Price</th>'+
                              '<th width="30%">Remarks</th>'+
                          '</tr>'+
                    '</thead>'+
                    '<tbody>'+
                      rows +
                    '</tbody>'+
                '<table><br /><br /><br />';
}

一旦我完成第 16 行的循环,我想在第 36 行返回变量 data 。但似乎它返回 null。有人可以帮助我纠正我的代码,因为我刚刚学习生成jquery 中的表。任何帮助将不胜感激。谢谢大家

最佳答案

$.getJSON() 异步返回结果。您可以在 $.each() 之后从 $.getJSON() 调用中返回 html 字符串和 data;使用.then()处理结果

function format ( d ) {
  var id = d[1],$base = '<?php echo Routed::url('/api/course_fees_setup/getSponsorList?'); ?>';
   var rows = ''; 
    return $.getJSON($base+'id=' + id)
    .then(function(data){
        var index =0;
        var newOptions = data.data;
        var SponsorbyFee = newOptions.Sponsorship[0].SponsorbyFee;
        $.each(SponsorbyFee, function(value,key) {
          // console.log(key);
          switch(key['fee_type_id']){
            case '1' :
                  // console.log('program');
                  break;
            case '2' :
                  // console.log(key['OthersFee']);
                  rows[index] = '<tr><td>' + key['OthersFee']['payment_description'] + '</td><td>' + key['OthersFee']['price'] + '</td><td>' + key['OthersFee']['remarks'] + '</td><tr>';
                  index++;
                  break;
            case '3' : 
                  // console.log(key['DiscountFee']);
                  rows[index] = '<tr><td>' + key['DiscountFee']['payment_description'] + '</td><td>' + key['DiscountFee']['price'] + '</td><td>' + key['DiscountFee']['remarks'] + '</td><tr>';
                  index++;
                  break;
          }
        });
        return ['<br /><br /><table id="sponsorlist-tbl" class="hover cell-border" width="100%">'+
                    '<thead>'+
                          '<tr>'+
                              '<th width="30%">Payment Description</th>'+
                              '<th width="10%">Price</th>'+
                              '<th width="30%">Remarks</th>'+
                          '</tr>'+
                    '</thead>'+
                    '<tbody>'+
                      rows +
                    '</tbody>'+


         '<table><br /><br /><br />'
         , data];
    });

}

format(/* parameter */).then(function(results) {
  console.log(results)
})

关于javascript - getJson之后如何携带数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717183/

相关文章:

javascript - 创建多个位于彼此之上的可放置 sibling

javascript - 如何在 JavaScript 中使用过滤函数实现Continue语句

jquery - 为什么 jQuery 中没有 $ ('.someSelector' ).id() 方法?

json - 在Grails View 中渲染Json响应

javascript - parseInt()/parseFloat() 忽略非数字参数

javascript - 如何在 JavaScript 中推送数组中的不同元素

php - jquery ajax 即使发生更新也总是出错

jquery - 尝试在 Html.DropDownFor 帮助程序中从 'onchange' 触发 JQuery

javascript - 使用来自延迟的 JSON 请求的方法和类创建 Javascript 对象

javascript - 即使只有一个被选中,jQuery 也会返回所有选中的复选框