【发布时间】:2018-03-28 09:52:43
【问题描述】:
我正在尝试使用 JavaScript 开发网站,
目标是获取xml文件的数据,并将其转换为vars。
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;
我已经尝试将xmlDoc = xml.responseXML; 替换为xmlDoc = this.responseXML;
但我得到 getElementByTagName 的一些错误:Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined
感谢您的帮助! 雅克
编辑:代码来源:Google Doc 是的,现场演示对我有用,我使用的是谷歌浏览器
【问题讨论】:
-
您的代码对我来说很好用。这里is a live demo 表明它也有效。现场演示对您有用吗?如果是这样,问题出在您没有向我们展示的代码中。否则:您使用的是哪个网络浏览器?
-
猜猜下一个问题是,哪个浏览器?
-
代码来源:Google Doc 是的,现场演示适合我,我使用的是谷歌浏览器
标签: javascript xml