Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用AJAX显示JSON数据内部数组的数据?_Javascript_Json_Ajax_Datatable - Fatal编程技术网

Javascript 如何使用AJAX显示JSON数据内部数组的数据?

Javascript 如何使用AJAX显示JSON数据内部数组的数据?,javascript,json,ajax,datatable,Javascript,Json,Ajax,Datatable,我有一些 [ { "selectionId":1, "selectionDate":"101662", "selectedBy":"ABC", "eximPanNo":222, "eximPanName":"DEF", "eximPanNameEng":"KKK", "eximPanAddress":null, "eximPanAddressEng":null, "eximP

我有一些

[  
   {  
      "selectionId":1,
      "selectionDate":"101662",
      "selectedBy":"ABC",
      "eximPanNo":222,
      "eximPanName":"DEF",
      "eximPanNameEng":"KKK",
      "eximPanAddress":null,
      "eximPanAddressEng":null,
      "eximPanPhone":12334566,
      "selectionType":"G",
      "consignmentNo":0,
      "consignmentDate":"2098",
      "productName":"LLL",
      "selectionFromDate":"2019",
      "selectionToDate":"2090",
      "agentNo":123,
      "selectionStatus":"I",
      "entryBy":"PCS",
      "entryDate":"2018-11-22 11:46:02",
      "rStatus":"F",
      "custOfficeId":1,
      "selectionAudit":[  
         {  
            "audGrpId":1,
            "selectionId":1,
            "assignFromDate":"2075-08-03",
            "assignToDate":"2075-08-19",
            "entryBy":"1",
            "rStatus":"1"
         }
      ]
   }
]
从AJAX调用时,如何在dataTable中显示此
selectionAudi.audGrpId
数据? 这里通过AJAX调用Api

var table = $('#nepal').DataTable({
            "processing" : true,
            "ajax" : {
                "url" : A_PAGE_CONTEXT_PATH + "/form/api/getAllSelectionAudit/all",
                dataSrc : ''
            },
            "columns" : [ {
                "data" : "selectionId"
            }, {
                "data" : "selectionDate"
            }, {
                "data" : "selectedBy"
            }, {
                "data" : "eximPanNo"
            } ]
        });
但是,当我添加“data”:“selectionaoudi.audGrpId”时,数据表会显示如下错误:

表的代码为:

<table id="nepal" class="table table-bodered">

          <thead>
            <tr>
              <th>Selection No</th>
              <th>SelectionDate</th>
              <th>SelectedBy</th>
              <th>PanEximNumber</th>
              <th>AudiGroupID</th>  


            </tr>
          </thead>
          <tbody>

          </tbody>

        </table> 

选择号
选择日期
选择人
PanEximNumber
AudiGroupID

如何将内部Json数据显示到datatable中?我看不出真正的解决方案是什么。

您的问题是
selectionAudit
是一个
数组,其中有一个
对象
包含
audGrpId
属性,因此只编写
selectionAudi.audGrpId
就是引发此错误的原因,因为它试图访问数组中的
audGrpId
属性

您需要编写
selectionAudit[0].audGrpId
以访问正确的属性

这是您的代码应该如何编写的:

var table = $('#nepal').DataTable({
        "processing" : true,
        "ajax" : {
            "url" : A_PAGE_CONTEXT_PATH + "/form/api/getAllSelectionAudit/all",
            dataSrc : ''
        },
        "columns" : [ {
            "data" : "selectionId"
        }, {
            "data" : "selectionDate"
        }, {
            "data" : "selectedBy"
        }, {
            "data" : "eximPanNo"
        }, {
            "data" : "selectionAudit[0].audGrpId"
         ]
});
注意:


这假设
selectionAudit
是一个
数组
,它总是填充着这个
对象

您的问题是
selectionAudit
是一个
数组
,其中有一个
对象
,该对象包含
audGrpId
属性,因此,仅仅编写
selectionAudi.audGrpId
就是引发此错误的原因,因为它试图访问数组中的
audGrpId
属性

您需要编写
selectionAudit[0].audGrpId
以访问正确的属性

这是您的代码应该如何编写的:

var table = $('#nepal').DataTable({
        "processing" : true,
        "ajax" : {
            "url" : A_PAGE_CONTEXT_PATH + "/form/api/getAllSelectionAudit/all",
            dataSrc : ''
        },
        "columns" : [ {
            "data" : "selectionId"
        }, {
            "data" : "selectionDate"
        }, {
            "data" : "selectedBy"
        }, {
            "data" : "eximPanNo"
        }, {
            "data" : "selectionAudit[0].audGrpId"
         ]
});
注意:


这假设
selectionAudit
是一个
数组
,并且它总是填充着这个
对象

,您可以按照我通常的方式来做,而不是服务器端处理数据表

假设您正在进行API调用的HTML页面-

<!-- Button to make API request -->
<button id="btnAPIRequest">API Request</button>

<!-- API response div -->
<div id="responseAPI"></div>

API请求
通过AJAX GET方法调用API请求,并在HTML页面底部添加以下javascript代码-

<script>
    $("#btnAPIRequest").click(function(){
        $.ajax({
            type: "GET",
            url: "Your/url/to/page.php",
            data: 'if you have any data else blank',
            dataType: 'json',
            success: function(data){
                if(data.success){
                    $("#responseAPI").html(data.message);

                    // Initialize datatable here
                }
                else{
                    $("#responseAPI").html(data.message);
                }
            }
        });
    });
</script>

$(“#btnAPIRequest”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“您的/url/to/page.php”,
数据:'如果您有任何其他数据为空',
数据类型:“json”,
成功:功能(数据){
if(data.success){
$(“#responseAPI”).html(data.message);
//在此初始化数据表
}
否则{
$(“#responseAPI”).html(data.message);
}
}
});
});
现在在php页面中,您实际调用API并解码json响应-

<?php  
// Load the reuiqred library

// Make API request

// Get the resonse in json
$response = '[{"selectionId":1,..."selectionAudit":[{"audGrpId":1,..."rStatus":"1"}]}]';

// Decode the json response
$decoded_data = json_decode($response);

// Get your html table
$msg = '
    <table>
        <thead>
            <tr>
              <th>Selection No</th>
              <th>SelectionDate</th>
              <th>SelectedBy</th>
              <th>PanEximNumber</th>
              <th>AudiGroupID</th>  
            </tr>
          </thead>
          <tbody>
';

// Table body data
foreach($decoded_data as $data){
    $msg .= '
        <tr>
            <td>'.$data[0].'</td>
            <td>'.$data[2].'</td>
            <td>'.$data[3].'</td>
            <td>'.$data[4].'</td>
    ';

    // I think here you got stuck when extracting from multidimensional array
    foreach($data[n] as $audi_data){
        $msg .= '<td>'.$audi_data[i].'</td>';
    }

    $msg .=  '</tr>'; 
}

$msg .= '</tbody></table>';

// For success respone
// encode this value in json and ajax response will handle this as per return datatype option
echo json_encode(array('success'=>true, 'message'=>$msg));

// Similarly  for failed response
echo json_encode(array('success'=>true, 'message'=>$msg));

?>

您可以按照我通常的方式进行操作,而不是在服务器端处理数据表

假设您正在进行API调用的HTML页面-

<!-- Button to make API request -->
<button id="btnAPIRequest">API Request</button>

<!-- API response div -->
<div id="responseAPI"></div>

API请求
通过AJAX GET方法调用API请求,并在HTML页面底部添加以下javascript代码-

<script>
    $("#btnAPIRequest").click(function(){
        $.ajax({
            type: "GET",
            url: "Your/url/to/page.php",
            data: 'if you have any data else blank',
            dataType: 'json',
            success: function(data){
                if(data.success){
                    $("#responseAPI").html(data.message);

                    // Initialize datatable here
                }
                else{
                    $("#responseAPI").html(data.message);
                }
            }
        });
    });
</script>

$(“#btnAPIRequest”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“您的/url/to/page.php”,
数据:'如果您有任何其他数据为空',
数据类型:“json”,
成功:功能(数据){
if(data.success){
$(“#responseAPI”).html(data.message);
//在此初始化数据表
}
否则{
$(“#responseAPI”).html(data.message);
}
}
});
});
现在在php页面中,您实际调用API并解码json响应-

<?php  
// Load the reuiqred library

// Make API request

// Get the resonse in json
$response = '[{"selectionId":1,..."selectionAudit":[{"audGrpId":1,..."rStatus":"1"}]}]';

// Decode the json response
$decoded_data = json_decode($response);

// Get your html table
$msg = '
    <table>
        <thead>
            <tr>
              <th>Selection No</th>
              <th>SelectionDate</th>
              <th>SelectedBy</th>
              <th>PanEximNumber</th>
              <th>AudiGroupID</th>  
            </tr>
          </thead>
          <tbody>
';

// Table body data
foreach($decoded_data as $data){
    $msg .= '
        <tr>
            <td>'.$data[0].'</td>
            <td>'.$data[2].'</td>
            <td>'.$data[3].'</td>
            <td>'.$data[4].'</td>
    ';

    // I think here you got stuck when extracting from multidimensional array
    foreach($data[n] as $audi_data){
        $msg .= '<td>'.$audi_data[i].'</td>';
    }

    $msg .=  '</tr>'; 
}

$msg .= '</tbody></table>';

// For success respone
// encode this value in json and ajax response will handle this as per return datatype option
echo json_encode(array('success'=>true, 'message'=>$msg));

// Similarly  for failed response
echo json_encode(array('success'=>true, 'message'=>$msg));

?>


selectionAudit
是一个
数组
,其中一个
对象
具有
audGrpId
属性,因此您需要
selectionAudi[0]。audGrpId
来访问该属性。因为它是一个数组,我建议您使用自定义
render
回调,使用
数据
参数并检查对象中是否存在
selectionAudit
。检查此项以供参考:
selectionAudit
是一个
数组
,其中一个
对象
具有
audGrpId
属性,因此您需要
selectionAudi[0]。audGrpId
来访问该属性。因为它是一个数组,我建议您使用自定义
render
回调,使用
数据
参数并检查对象中是否存在
selectionAudit
。检查此项以供参考:只是一个旁注:如果
selectionAudit
为空数组,则此操作将失败。不确定这是否会发生,但仍然…@briosheje是的,你是对的,但我假设它总是这样。只是一个旁注:如果
selectionAudit
是一个空数组,这将失败。不确定这是否会发生,但仍然…@briosheje是的,你是对的,但我认为它总是这样。