"语法错误:在属性列表"之后丢失}如何使Javascript符号证明?

时间:2016-01-29 09:04:15

标签: javascript php

我有这个JS片段,它在地图上显示带有描述的图标。偶尔地图不显示,我有

"语法错误:在属性列表"之后丢失}

在Firefox中进行调查时。 我无法在代码中找到错误,所以我发现这可能是由于变量名中的特殊符号,如:"," "()"等等。有没有办法使这个代码符号符号?



// <![CDATA[
 jQuery(document).ready(
   function() {
     jQuery("#mapobjects").GoogleMaps({
       "dataSource": "Recordset",
       "dataSourceType": "dynamic",
       "zoom": "fit",
       "markers": [ <? php
         do { ?> {
             "latitude": <? php echo $row_Recordsetmapa['object_lat']; ?> , 
             "longitude": <? php echo $row_Recordsetmapa['object_lng']; ?> , 
             "html": "<a href='Obiekt.php?OID=<?php echo $row_Recordsetmapa['ID_object']; ?>'><?php echo        $row_Recordsetmapa['object_name']; ?>",
             "title": "<?php echo $row_Recordsetmapa['object_name']; ?>",
             "icon": {
             "image": "images//m<?php echo $_GET['Cat']; ?>.png",
             "iconsize": [70, 70],
             "iconanchor": [13, 26],
             }
           }, <? php
         } while ($row_Recordsetmapa = mysql_fetch_assoc($Recordsetmapa)); ?>
       ]
     });
   }
 );
 // ]]>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

用json_encode创建标记Javascript数组手动创建这样的数据不是一个好方法。请在此链接JSON.parse() isn't working

上查看我的回答

以下是您问题的一个虚拟示例;

<?PHP
    $markerObject = new stdClass();
    $markerObject->latitude = "41.015137";
    $markerObject->longitude = "28.979530";
    $markerObject->html = "<a href=\"href...\">Bla bla...</a>";
    $markerObject->title = "Marker title";
    $markerObject->icon = new stdClass();
    $markerObject->icon->image = "path_of_image.png";
    $markerObject->icon->iconsize = array();
    $markerObject->icon->iconsize[] = 70;
    $markerObject->icon->iconsize[] = 70;
    $markerObject->icon->iconanchor = array();
    $markerObject->icon->iconanchor[] = 13;
    $markerObject->icon->iconanchor[] = 26;

    $markers = array();
    for($i=0;$i<=4;$i++)
    {
        $markers[] = $markerObject;
    }

    $markersJson = json_encode($markers);

?>

上面的PHP代码的工作示例在https://ideone.com/APxfm9,您的javascript代码应如下所示;

// <![CDATA[
 jQuery(document).ready(
   function() {
     jQuery("#mapobjects").GoogleMaps({
       "dataSource": "Recordset",
       "dataSourceType": "dynamic",
       "zoom": "fit",
       "markers": <?PHP echo $markersJson; ?>
     });
   }
 );
 // ]]>