简体   繁体   English

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

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

I've been looking for hours and I can't find an answer to this. 我已经找了几个小时,却找不到答案。

I'm sending this array 我正在发送这个数组

$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'];

endoded in a json using this code: 使用以下代码嵌入json:

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

$c = json_encode($list);

echo $c;

I want to display it on an html div. 我想在html div上显示它。 For that I'm using this code: 为此,我使用以下代码:

success: function(msg,string,jqXHR){

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

This doesn't work, and the error I'm getting is: 这不起作用,我得到的错误是:

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. 未捕获的TypeError:无法在“节点”上执行“ appendChild”:参数1的类型不是“节点”。

I'm guessing this doesn't work because I either can't encode it like that or I can't append it like that. 我猜这是行不通的,因为我要么不能那样编码,要么不能那样附加。 Is there any way of doing this? 有什么办法吗? Thanks 谢谢

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

{"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"]} { “小时”: “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 is an array and jQuery's .html() method accepts a string as a parameter. msg.hours是一个数组,jQuery的.html()方法接受字符串作为参数。

You should convert the array to string first by toString() method. 您应该首先通过toString()方法将数组转换为字符串。

Try this: 尝试这个:

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

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

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