2016-09-20 70 views
1

我想填充在其上的一个模式绘制一个DataTable我的数据,我搜索了很多环节和我遇到一个有趣的话题来了..填充在数据表中的数据不能正常工作

我有一个按钮将触发该函数以获取从servlet数据..

<button class="w3-btn w3-black w3-round-xxlarge w3-hover-green" id="viewButton" onClick="loadDoc(this.id)">View</button> 

我的AJAX代码..

<script> 
function loadDoc(id) { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     openModal(this.responseText); 
    } 
    }; 
    xhttp.open("GET", "/ETEEAP/ViewApplication?id=" + id, true); 
    xhttp.send(); 
} 
function openModal(id){ 
document.getElementById('id01').style.display='block'; 
loadTable(id); 
} 
</script> 

我能打开模式,但它返回“请求未知”的错误 http://datatables.net/tn/4为错误的详细信息...

这是我的模态代码..

<div id="id01" class="w3-modal"> 
<div class="w3-modal-content w3-animate-top w3-card-8" style="margin-top:20px;"> 
<header class="w3-container w3-teal"> 
    <span onclick="document.getElementById('id01').style.display='none';" 
    class="w3-closebtn">&times;</span> 
    <h2>Program Details</h2> 
</header> 
<div class="w3-container w3-light-grey" style="margin-bottom: 50px;"> 
    <div class="w3-container w3-padding-8 w3-opacity w3-white w3-round-xlarge w3-border w3-hover-border-black" 
    style="margin: 10px 10px 10px 10px;"> 
     <table id="myTable1" class="display"> 
     <thead> 
     <tr> 
      <th>SUBJECT</th> 
      <th>COURSE</th> 
      <th>UNITS</th> 
      <th>SEMESTER</th> 
      <th>YEAR LEVEL</th> 
      <th>STATUS</th> 
     </tr> 
     </thead> 
     <tfoot> 
     <tr> 
      <th>SUBJECT</th> 
      <th>COURSE</th> 
      <th>UNITS</th> 
      <th>SEMESTER</th> 
      <th>YEAR LEVEL</th> 
      <th>STATUS</th> 
     </tr> 
     </tfoot> 
     <tbody> 
    </tbody> 
    </table> 
    <script> 
     function loadTable(id){ 
      alert(id); 
      $('#myTable1').DataTable({ 
       aaData : id, 
       aoColumns : [ 
        {mDataProp : "SUBJECT"}, 
        {mDataProp : "COURSE"}, 
        {mDataProp : "UNITS"}, 
        {mDataProp : "SEMESTER"}, 
        {mDataProp : "YEAR LEVEL"}, 
        {mDataProp : "STATUS"} 
          ] 
      }); 
     } 
    </script> 
    </div> 
</div> 

这是我从servlet得到的回应..

[{"SUBJECT":"Programming I","UNITS":"3","SEMESTER":"First","COURSE":"BSCPE","YEAR LEVEL":"First","STATUS":"PENDING"}, {"SUBJECT":"Communication Arts I","UNITS":"2","SEMESTER":"First","COURSE":"BSCPE","YEAR LEVEL":"First","STATUS":"PENDING"}, {"SUBJECT":"Programming II","UNITS":"3","SEMESTER":"Second","COURSE":"BSCPE","YEAR LEVEL":"First","STATUS":"PENDING"}, {"SUBJECT":"COMORG","UNITS":"4","SEMESTER":"Second","COURSE":"BSCPE","YEAR LEVEL":"Second","STATUS":"PENDING"}] 

当我运行这个程序时,它抛出上面的错误..但是当我分配和硬编码指定的响应变量上它工作正常..如何它不工作?谁能帮助我..

+0

你可以发布'loadTable'函数吗? –

+0

它对模式代码..:D –

+0

你的servlet响应是字符串还是JSON对象? – CMedina

回答

1

这是现在的工作。我只是转换成JSON响应..

ID = $ .parseJSON(ID);