0

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"]}

10
  • 1
    Have you tried a simple consol.log(JSON.stringify(msg)) to confirm you are getting back from the json service what you expect? Commented Apr 20, 2016 at 15:22
  • What does msg.hours contain? Commented Apr 20, 2016 at 15:25
  • msg.hours contains the array $dailyHours, or at least it should. I'll try running the console log to see what I'm getting. edit: I'm getting the array, so it has to be something wrong with how I'm trying to display it. Commented Apr 20, 2016 at 15:26
  • Try doing $("#result").text(msg.hours);... Commented Apr 20, 2016 at 15:26
  • Possible duplicate of What is the difference between jQuery: text() and html() ? Commented Apr 20, 2016 at 15:27

1 Answer 1

5

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.