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

A.Draug
  • 43
  • 7
  • 1
    Have you tried a simple consol.log(JSON.stringify(msg)) to confirm you are getting back from the json service what you expect? – mba12 Apr 20 '16 at 15:22
  • What does `msg.hours` contain? – brso05 Apr 20 '16 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. – A.Draug Apr 20 '16 at 15:26
  • Try doing `$("#result").text(msg.hours);`... – brso05 Apr 20 '16 at 15:26
  • Possible duplicate of [What is the difference between jQuery: text() and html() ?](http://stackoverflow.com/questions/1910794/what-is-the-difference-between-jquery-text-and-html) – brso05 Apr 20 '16 at 15:27
  • @brso05 When I run that, I have this error: Uncaught TypeError: Cannot read property 'length' of undefined – A.Draug Apr 20 '16 at 15:29
  • @A.Draug I don't see `length` anywhere in your code... – brso05 Apr 20 '16 at 15:33
  • @A.Draug did you log `msg.hours`? What are the contents of `msg.hours` in the console? – brso05 Apr 20 '16 at 15:34
  • Neither do I, I have no idea why I'm getting this error. If I try to display a single string, it works fine, when I try the array, I get the error. Adding the content of msg.hours to main post – A.Draug Apr 20 '16 at 15:35
  • @A.Draug Maybe try `$("#result").text("" + msg.hours.toString());` – brso05 Apr 20 '16 at 15:37

1 Answers1

4

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());
George Pant
  • 2,079
  • 1
  • 9
  • 14