2014-12-05 63 views
0

我的ajax脚本发送json对象给浏览器,但表无法加载json对象。 我的Ajax脚本:bootstrap表无法加载json对象

$.ajax({ 
      type : "POST", 
      url : "getLabels.jsp", 
      data : "mailingID=" + selectedValue, // posCodeSelected 
      success : function(data) { 

       response = $.parseJSON(data);// this statement sends data succesfully to browser 

      }, 
      error : function(response) { 
       var responseTextObject = jQuery.parseJSON(response.responseText); 
      } 
     }); 

这是我举表嵌入到我的jsp页面。

<table data-height="299" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1"> 
    <thead> 
    <tr> 
     <th data-field="rowNumber" >ID</th> 
     <th data-field="firstname" >first name</th> 
     <th data-field="lastname" >last name</th> 
     <th data-field="organization" >organization</th> 
     <th data-field="city" >city</th> 
     <th data-field="state" >state</th> 

    </tr> 
    </thead> 
</table> 

只是为了让你们现在这是浏览器的json响应:

{"rowNumber":1,"mailingID":3,"firstname":"Brian","lastname":"Fairhurst","organization":"Florida State University","city":"Tallahassee","state":"FL"} 
+0

什么是你用'response'你解析JSON后做什么? – 2014-12-05 18:33:00

+0

此主题可能对您有帮助吗? http://stackoverflow.com/questions/25187349/bootstrap-table-showing-json-data – 2014-12-05 18:33:15

+0

@BlaiseSwanwick:是的,你提供的链接有加载json文件的表属性data-url =“test.json”。但在我的情况下,我不得不将json对象(响应)加载到表中。 – nikhilk 2014-12-05 18:38:42

回答

0

您必须添加一个新行JSON对象返回:

//you need to set id="tbl" to your table on the html 

var table = document.getElementById("tbl"); 

var row = table.insertRow(table.rows.length); //insert a new row 

// insert new cells with info 

cells = []; 

for(var i = 0;i < 6;i++){ 
    cells[i] = row.insertCell(i); 
} 

// add the information store in json object 

cells[0].innerHTML = response.rowNumber; 
cells[0].innerHTML = response.firstname; 
cells[0].innerHTML = response.lastname; 
cells[0].innerHTML = response.organization; 
cells[0].innerHTML = response.city; 
cells[0].innerHTML = response.state; 
0

问题是HTML表格未被填入response中的数据。将data-url属性添加到HTML表格中应该可行。

因此,而不是:

<table data-height="299" data-show-refresh="true" ...> 
    <thead> 
    ... 

你想做的事:

<table data-height="299" data-show-refresh="true" data-url="response"...> 
    <thead> 
    ...