繁体   English   中英

从php编码的json检索数组并将其附加到div主体

[英]retrieve an array from a php encoded json and append it to a div body

我已经找了几个小时,却找不到答案。

我正在发送这个数组

$dailyHours = ['8:00','8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30'
,'15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30'];

使用以下代码嵌入json:

$list = array('hours' => $dailyHours);

$c = json_encode($list);

echo $c;

我想在html div上显示它。 为此,我使用以下代码:

success: function(msg,string,jqXHR){

                    $("#result").html(msg.hours);
                }

这不起作用,我得到的错误是:

未捕获的TypeError:无法在“节点”上执行“ appendChild”:参数1的类型不是“节点”。

我猜这是行不通的,因为我要么不能那样编码,要么不能那样附加。 有什么办法吗? 谢谢

编辑:console.log(msg.hours)的结果

{ “小时”: “8:00”, “8:30”, “9:00”, “9:30”, “10:00”, “10:30”, “11:00”,“11 :30" , “12:00”, “12:30”, “13:00”, “13:30”, “14:00”, “14:30”, “15:00”,“15:30 ”, “16:00”, “16:30”, “17:00”, “17:30”, “18:00”, “18:30”, “19:00”, “19:30”] }

msg.hours是一个数组,jQuery的.html()方法接受字符串作为参数。

您应该首先通过toString()方法将数组转换为字符串。

尝试这个:

       $("#result").html(msg.hours.toString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM