简体   繁体   English

将数据PHP JSON解析为Ajax-我错过了什么吗?

[英]Parsed data PHP JSON to Ajax - have I missed something?

I have a issue with my code and I don't know what the problem is. 我的代码有问题,我不知道问题是什么。 I'm trying to move markers in a map with coordinates of the bus fleet. 我正在尝试在带有公交车队坐标的地图中移动标记。 My method is to generate JSON with PHP and an Oracle database, then in JavaScript use the data to print a move of the markers...but I think the information is not getting through to the web map. 我的方法是使用PHP和Oracle数据库生成JSON,然后在JavaScript中使用数据来打印标记的移动...但是我认为信息无法传递到Web地图。

Please help. 请帮忙。

The php (marks.php): 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: 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 to return value1, but it returns undefined instead.
          }                 
        });

        </script>


</html>

I'm trying to at least alert data but nothing happened. 我正在尝试至少警报数据,但没有任何反应。 Please help me to see what my problem is. 请帮助我看看我的问题是什么。

You're fetching in a loop, and outputting JSON on every iteration. 您正在循环获取,并在每次迭代中输出JSON。 That's invalid. 那是无效的。 You're essentially producing this: 您实际上是在生产此代码:

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

which is illegal JSON. 这是非法的JSON。 You need to build an array inside the loop, then encode it AFTER the loop finishes: 您需要在循环内部构建一个数组,然后在循环完成后对其进行编码:

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

Since your JSON is illegal, javascript isn't decoding it at all (well, starts to decode it, then aborts once it hits the syntax errors). 由于您的JSON非法,因此javascript根本不会对其进行解码(嗯,开始对其进行解码,然后在遇到语法错误时中止操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM