无法找出此错误 - 无法读取 null 的属性'documentElement'



我有以下XML文件 -

<marker>
<marker id="1" name="Graeme Hall Roundabout" address="Graeme Hall Roundabout" lat="13.077160" lng="-59.568535" type="suv"/>
<marker id="2" name="Deighton Griffith Junction" address="Deighton Griffith Junction" lat="13.080240" lng="-59.550358" type="suv"/>
<marker id="3" name="Airport Roundabout" address="Airport Roundabout" lat="13.080238" lng="-59.491413" type="zr"/>
<marker id="4" name="Waterford Bottom" address="Waterford Bottom" lat="13.120412" lng="-59.599075" type="zr"/>
</marker>

并且正在使用本教程以下脚本来阅读它 -

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Using MySQL and PHP with Google Maps</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var customLabel = {
        restaurant: {
          label: 'R'
        },
        bar: {
          label: 'B'
        }
      };
        function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: new google.maps.LatLng(13.179190,-59.561025),
          zoom: 12
        });
        var infoWindow = new google.maps.InfoWindow;
          // Change this depending on the name of your PHP or XML file
          downloadUrl('xml.php', function(data) {
            var xml = data.responseXML;
            ***var markers = xml.documentElement.getElementsByTagName('marker');***
			Array.prototype.forEach.call(markers, function(markerElem) {
              var id = markerElem.getAttribute('id');
              var name = markerElem.getAttribute('name');
              var address = markerElem.getAttribute('address');
              var type = markerElem.getAttribute('type');
              var point = new google.maps.LatLng(
                  parseFloat(markerElem.getAttribute('lat')),
                  parseFloat(markerElem.getAttribute('lng')));
              var infowincontent = document.createElement('div');
              var strong = document.createElement('strong');
              strong.textContent = name
              infowincontent.appendChild(strong);
              infowincontent.appendChild(document.createElement('br'));
              var text = document.createElement('text');
              text.textContent = address
              infowincontent.appendChild(text);
              var icon = customLabel[type] || {};
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                label: icon.label
              });
              marker.addListener('click', function() {
                infoWindow.setContent(infowincontent);
                infoWindow.open(map, marker);
              });
            });
          });
        }
      function downloadUrl(url, callback) {
        var request = window.ActiveXObject ?
            new ActiveXObject('Microsoft.XMLHTTP') :
            new XMLHttpRequest;
        request.onreadystatechange = function() {
          if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
          }
        };
        request.open('GET', url, true);
        request.send(null);
      }
      function doNothing() {}
    </script>
    <script async defer
     src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBoE2qN1puD-faj_5lIC_O464doKaoJDs0&callback=initMap">
    </script>
  </body>
</html>

但是,我在执行时收到以下错误。

xml.html:45 Uncaught TypeError: Cannot read property 'documentElement' of null
    at xml.html:45
    at XMLHttpRequest.request.onreadystatechange (xml.html:88)

我已经验证了XML文件并且它很好,所以我不明白为什么错误继续发生。

任何协助将不胜感激。

谢谢

史蒂夫

更改

新的XMLHttpRequest;

new XMLHttpRequest((;

downloadUrl()功能中。

在 Javascript 中,您可以使用您可能知道的functionName()调用函数或调用函数构造函数 - 我相信这只是您的拼写错误。

还要确保您的服务器返回 xml 响应。

最新更新