未捕获的TypeError:无法读取未定义的属性'responseXML'

问题描述 投票:1回答:1

我正在尝试使用JavaScript开发网站,

目标是获取xml文件的数据,并将其转换为变量。

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        initMap(this);
    }
};

xhttp.open("GET", "https://applications002.brest-metropole.fr/WIPOD01/Transport/REST/getGeolocatedVehiclesPosition?format=xml&route_id=1&trip_headsign=Fort%20Montbarey", true);

xhttp.send();

function initMap(xml) {
    var xlat, xlon, i, xmlDoc;
    xmlDoc = xml.responseXML;
    xlat = xmlDoc.getElementsByTagName('Lat');
    xlon = xmlDoc.getElementsByTagName('Lon');
    /*
    xlat = xml.responseXML.getElementsByTagName('lat');
    xlon = xml.responseXML.getElementsByTagName('lon');
    */
    for (i = 0; i < xlat.length; i++) {
        var latcode = xlat[i].childNodes[0].nodeValue;
        var loncode = xlon[i].childNodes[0].nodeValue;
        //var myLatLng = "{lat: " + latcode + ", lng: " + loncode +"}";
        //var myLatLng = {lat: latcode, lng: loncode};
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(latcode, loncode),
            map: map,
        });
    }
    [...]
}

但是我得到了这个代码错误:Uncaught TypeError: Cannot read property 'responseXML' of undefined @ - > xmlDoc = xml.responseXML;

我已经尝试用qazxsw poi取代qazxsw poi

但我得到getElementsByTagName:qazxsw poi的一些错误

感谢帮助 !雅克·

编辑:代码来源:xmlDoc = xml.responseXML;是的现场演示适用于我,我正在使用谷歌浏览器

javascript xml
1个回答
0
投票

对于xmlDoc = this.responseXML;错误,XML文件中的一个条目很可能没有您要查找的标记值。扫描xml并确保每个条目中都有该标记的值。

我不知道如何处理一个不完整的文件。

至于Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined,我在许多页面上得到了它,似乎工作得非常好。到了我现在忽略它的程度;但我希望有人能够解释它,以便我可以解决任何潜在的问题。

© www.soinside.com 2019 - 2024. All rights reserved.