未捕获的SyntaxError:意外的令牌

时间:2012-12-26 08:25:36

标签: javascript json

从这个错误中惹恼了/////我试图从数据库中获取langitude和lattitude,并根据他们想要在地图上显示标记.....并且面对那个错误....请帮助我....感谢名单 我的代码是:

    // Ban Jelacic Square - City Center
            var map;
    var center = new google.maps.LatLng(-34.397, 150.644);

    var geocoder = new google.maps.Geocoder();
    var infowindow = new google.maps.InfoWindow();

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();

    function init() {

        var mapOptions = {
          zoom: 8,
          center: center,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('directions_panel'));

    /*  // Detect user location
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {

                var userLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

                geocoder.geocode( { 'latLng': userLocation }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        document.getElementById('start').value = results[0].formatted_address;
                    }
                });

            }, function() {
                alert('Geolocation is supported, but it failed');
            });
        }
        */
        makeRequest('get_location.php', function(data) {
            var data = JSON.parse(data.responseText);
            for (var i = 0; i < data.length; i++) {
            displayLocation(data[i]);
            }
        });
    }

    function displayLocation(location) {

        var content =   '<div class="infoWindow"><strong>'  + location.name + '</strong>'
                        + '<br/>'   + location.address
                        + '<br/>'   + location.description + '</div>';

        if (parseInt(location.lat) == 0) {
            geocoder.geocode( { 'address': location.address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location,
                        title: location.name
                    });

                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(content);
                        infowindow.open(map,marker);
                    });
                }
            });
        } else {

            var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lng));
            var marker = new google.maps.Marker({
                map: map, 
                position: position,
                title: location.name
            });
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(content);
                infowindow.open(map,marker);
            });
        }
    }  
    function makeRequest(url, callback) {
        var request;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
        } else {
            request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
        }
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                callback(request);
            }
        }
        request.open("GET", url, true);
        request.send();
    }

///// get_location.php文件

setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);         $ sth = $ db-&gt; query(“SELECT * FROM data”);         $ locations = $ sth-&gt; fetchAll();         echo json_encode($ locations);     } catch(Exception $ e){         echo $ e-&gt; getMessage();     }     ?&GT;

1 个答案:

答案 0 :(得分:0)

试试这个,看看是否有帮助:

<?php
require 'config1.php';

function removeNonUTF8( &$value, $key ) {
    $regex = <<<'END'
/
  (
    (?: [\x00-\x7F]
    |   [\xC0-\xDF][\x80-\xBF]
    |   [\xE0-\xEF][\x80-\xBF]{2}
    |   [\xF0-\xF7][\x80-\xBF]{3}
    )+                              # ...one or more times
  )
| .                                 # anything else
/x
END;

    $value = preg_replace( $regex, '$1', $value );
}

try { 
    $db = new PDO($dsn, $uname, $pass); 
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 

    $sth = $db->query("SELECT * FROM data"); 
    $locations = $sth->fetchAll(); 

    foreach( $locations as $location ) {
        array_walk( $location, 'removeNonUTF8' );
    }

    echo json_encode( $locations ); 
} catch (Exception $e) { 
    echo $e->getMessage(); 
}
?>

此外,如果这不起作用,您可以打印$locations变量,以便我们可以看到结构。

希望这有帮助。