【问题标题】:How to display a table using an ajax request如何使用 ajax 请求显示表格
【发布时间】:2023-03-04 16:48:01
【问题描述】:

我从另一个用户的类似问题中获取了一个代码示例,这也部分与 JSON 相关。

$(document).ready(function() {
(function poll() {
$.ajax({
    url : 'http://localhost/scripts/query.php',
    type : 'POST',
    data : {},
    dataType:'json',
    success : function(response) {
    var json_obj = $.parseJSON(JSON.stringify(response));
    var output="";
    for (var i in json_obj) 
    {   
        output+="<tr>";
        output+="<td>" + json_obj[i].time.date + "</td>" + "<td>" + json_obj[i].username + "</td>" + "<td>" + json_obj[i].rics + "</td>" + "<td>" + json_obj[i].exclusive_rics +"</td>";
        output+="</tr>";
    }
    $('#table_content').html(output);
    },
    error : function(request,error)
    {
        alert("Request: "+JSON.stringify(request));
    } ,
    dataType: "json",
    complete: setTimeout(function() {poll()}, 5000),
    timeout: 2000
})
})();

我的问题是如何在我的 PHP 代码中发送 JSON 响应,以便我可以存储多个用户并打印多行这些数据。因此,例如,我有 Heather、Larry 和 George,并希望以上述方式打印与这些用户相关的所有信息。此外,对象的值如何能够存储“用户名”、“日期”等。

【问题讨论】:

标签: php json ajax html-table


【解决方案1】:

有一个json编码功能可以使用;

只需将您的返回值设置为

json_encode($array);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    相关资源
    最近更新 更多