【发布时间】: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