php - Why I didn't get JSON response? -


  <?php   include ('config.php');   $stringdata = $_post['datastring'];    $sql=mysql_query("select * comments post_id_fk='$stringdata'");    while($row=mysql_fetch_array($sql)) {       $user=$row['user_id'];       $time=$row['time'];       $comment=$row['comment_content'];       $respond=array(           'user'=>$user,           'time'=>$time,           'comment'=>$comment       );       echo   json_encode ($respond);   }   ?> 

i have script , can't firgure out, doesn't work here , why response isn't json?

in firebug shows response:

{"user":"890","time":"2013-08-15 20:34:02","comment":"what's up?"} {"user":"878","time":"2013-08-15 23:35:45","comment":"opa"}  

you might want combine json data single object/array before encoding it:

$output = array();  while($row=mysql_fetch_array($sql)) {       $user=$row['user_id'];       $time=$row['time'];       $comment=$row['comment_content'];       $respond=array(           'user'=>$user,           'time'=>$time,           'comment'=>$comment       );       $output[] =  json_encode ($respond); }  echo json_encode($output); 

also adding headers might help, if wrong content type detected:

header('content-type: application/json'); echo json_encode($output); 

just make sure set those, before echoing anything...


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -