javascript - Parsed data PHP JSON to Ajax - have I missed something? -


i have issue code , don't know problem is. i'm trying move markers in map coordinates of bus fleet. method generate json php , oracle database, in javascript use data print move of markers...but think information not getting through web map.

please help.

the php (marks.php):

query oracle....  while (($row = oci_fetch_array($result, oci_both)) != false) {   header('content-type: application/json');   $data = array($row);   echo json_encode($data);   }   

the result:

{"0":"10\/07\/15","max(op.telegramdate)":"10\/07\/15","1":"12115","busnumber":"12115","2":"511031","stopid":"511031","3":"-765320567","gpsx":"-765320567","4":"33334550","gpsy":"33334550","5":"a11","shortname":"a11"} 

the javascript:

<!doctype html> <html>   <head>     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">     <meta charset="utf-8">     <title>real time</title> <style>       html, body, #map {         height: 100%;         margin: 0px;         padding: 0px       }     </style>  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=true&amp;libraries=places&amp;language=es"> </script> <script src="http://190.216.202.35/prueba/js/mapmark.js"></script> </head>   <body >     <div id="map"></div>   </body>     <script>     var map;          map = new google.maps.map(document.getelementbyid("map"), {             center : new google.maps.latlng(3.4404103,-76.5077627),             zoom : 13,             maptypeid : 'roadmap'         });     $.ajax({                           type: "post",           datatype: "json",           url: "marks.php",                             cache: false,           success: function(data){             alert(data[0]); //expected return value1, returns undefined instead.           }                          });          </script>   </html> 

i'm trying @ least alert data nothing happened. please me see problem is.

you're fetching in loop, , outputting json on every iteration. that's invalid. you're producing this:

 {...}{...}{...}etc... 

which illegal json. need build array inside loop, encode after loop finishes:

$data = array(); while($row = fetch...) {    $data[] = $row; } echo json_encode($data); 

since json illegal, javascript isn't decoding @ (well, starts decode it, aborts once hits syntax errors).


Comments