【问题标题】:bootstrap table unable to load json objects引导表无法加载 json 对象
【发布时间】:2014-12-05 18:18:47
【问题描述】:

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

 $.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"}

【问题讨论】:

  • 解析 JSON 后,response 在做什么?
  • 这篇文章可能有帮助吗? stackoverflow.com/questions/25187349/…
  • @BlaiseSwanwick:是的,您提供的链接具有加载 json 文件的表属性 data-url="test.json"。但就我而言,我必须将 json 对象(响应)加载到表中。
  • 那我会回答的。很高兴有帮助。

标签: javascript ajax json twitter-bootstrap jsp


【解决方案1】:

您必须使用返回的 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;

【讨论】:

    【解决方案2】:

    问题在于 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>
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2016-10-25
      • 2016-04-08
      相关资源
      最近更新 更多