Using javascript to access a json array from php

Posted by celenius on Stack Overflow See other posts from Stack Overflow or by celenius
Published on 2011-01-15T20:24:06Z Indexed on 2011/01/15 20:53 UTC
Read the original article Hit count: 239

Filed under:
|

I'm trying to understand how my php script can pass an array to my javascript code. Using the following php, I pass an array:

 $c = array(3,2,7);

  echo json_encode($c);

My javascript is as follows:

$.post("getLatLong.php", { latitude: 500000},
   function(data){         

   arrayData = data
   document.write(arrayData)
   document.write(arrayData[0]); 
   document.write(arrayData[0]);
   document.write(arrayData[0]);

   });

</script>

What is printed out on screen is

[3,2,7][3,

I'm trying to understand how json_encode works - I though I would be able to pass the array to a variable, and then access it like a normal javascript array, but it views my array as one large text string. How do ensure that it reads it like an array?

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript