gpt4 book ai didi

php - 循环显示 JSON 数据

转载 作者:行者123 更新时间:2023-12-01 04:42:17 25 4
gpt4 key购买 nike

我需要在 PHP 中显示 JSON 数据。数据以循环形式出现。原始数据如下所示:

[{
"count": 3,
"not": "New Intender details entered"
},{
"count": 3,
"not": "New Intender details entered"
},{
"count": 3,
"not": "New Intender details entered"
}]

我的AJAX脚本如下:

function addmsg(type, msg) {
var obj = jQuery.parseJSON(msg);
$.each(obj, function(index, value) {
var count = value.count;
var not = value.not;
alert(not);
$('#msg_count').html(count);
$('#notification').html(not);
});
}

function waitForMsg(){
$.ajax({
type: "GET",
url: "notification/select.php",
cache: false,
timeout: 50000,
success: function(data){
addmsg("new", data);
setTimeout(waitForMsg, 1000);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(waitForMsg, 15000);
}
});
};

$(document).ready(function(){
waitForMsg();
});

我的 HTML 如下:

<span id="msg_count"></span>
<span id="notification"></span>

我面临的问题是msg_count显示良好。现在通知是一个循环,这意味着它包含多个记录。当我通过 span 调用它时,它只显示一条记录。其余记录未显示。我已经给了alert(not)这正在提醒所有记录。我唯一的问题是在 html 中显示为 <span id="notification"></span> 。谁能帮我吗?

最佳答案

如果通知显示循环数据,那么您必须添加所有 not in not 变量,然后将其显示在通知范围中。代码可以如下:

 function addmsg(type, msg) {
var obj = jQuery.parseJSON(msg);
var not = "";
$.each(obj, function(index, value) {
var count = value.count;
not += value.not+"<br/>";
$('#msg_count').html(count);

});
$('#notification').html(not);
}

关于php - 循环显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com