简体   繁体   中英

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:

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

$c = json_encode($list);

echo $c;

I want to display it on an 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'.

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)

{"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 is an array and jQuery's .html() method accepts a string as a parameter.

You should convert the array to string first by toString() method.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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