JS提示 Cannot read property '0' of undefined
index.html中functionprocessCity(){if(xmlHttpRequest.readyState==4){varvalueOfXml=xmlHt...
index.html中
function processCity() {
if (xmlHttpRequest.readyState == 4) {
var valueOfXml = xmlHttpRequest.responseXML;
var cities = valueOfXml.getElementsByTagName("city");
$("city").length = 1;
for (var i in cities) {
var getCities = cities[i].childNodes[0].nodeValue;
var cityOption = document.createElement("option");
cityOption.innerText = getCities;
cityOption.value = getCities;
$("city").appendChild(cityOption);
}
}
}
function getCity() {
xmlHttpRequest = getXmlHttpRequest();
var url = "showCity.jsp";
var provinceData = "item=province&province=" + $("province").value;
xmlHttpRequest.open("post", url, true);
xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpRequest.onreadystatechange = processCity;
xmlHttpRequest.send(provinceData);
}
showCity.jsp中
String item = request.getParameter("item");
if (item.equals("province")) {
String province = request.getParameter("province");
switch (province) {
case "山西":
out.println("<province>" +
"<city>太原市</city>" +
"<city>大同市</city>" +
"<city>阳泉市</city>" +
"<city>晋城市</city>" +
"<city>朔州市</city>" +
"<city>晋中市</city>" +
"<city>忻州市</city>" +
"<city>吕梁市</city>" +
"<city>长治市</city>" +
"<city>临汾市</city>" +
"<city>运城市</city>" +
"</province>");
break;
case "北京":
out.print("<province>" +
"<city>北京市</city>" +
"</province>");
break;
}
}
控制台报错:Uncaught TypeError: Cannot read property '0' of undefined at XMLHttpRequest.processCity 展开
function processCity() {
if (xmlHttpRequest.readyState == 4) {
var valueOfXml = xmlHttpRequest.responseXML;
var cities = valueOfXml.getElementsByTagName("city");
$("city").length = 1;
for (var i in cities) {
var getCities = cities[i].childNodes[0].nodeValue;
var cityOption = document.createElement("option");
cityOption.innerText = getCities;
cityOption.value = getCities;
$("city").appendChild(cityOption);
}
}
}
function getCity() {
xmlHttpRequest = getXmlHttpRequest();
var url = "showCity.jsp";
var provinceData = "item=province&province=" + $("province").value;
xmlHttpRequest.open("post", url, true);
xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttpRequest.onreadystatechange = processCity;
xmlHttpRequest.send(provinceData);
}
showCity.jsp中
String item = request.getParameter("item");
if (item.equals("province")) {
String province = request.getParameter("province");
switch (province) {
case "山西":
out.println("<province>" +
"<city>太原市</city>" +
"<city>大同市</city>" +
"<city>阳泉市</city>" +
"<city>晋城市</city>" +
"<city>朔州市</city>" +
"<city>晋中市</city>" +
"<city>忻州市</city>" +
"<city>吕梁市</city>" +
"<city>长治市</city>" +
"<city>临汾市</city>" +
"<city>运城市</city>" +
"</province>");
break;
case "北京":
out.print("<province>" +
"<city>北京市</city>" +
"</province>");
break;
}
}
控制台报错:Uncaught TypeError: Cannot read property '0' of undefined at XMLHttpRequest.processCity 展开
2个回答
展开全部
for (var i in cities)改为:for (var i=0;i<cities.length;i++ )。
for(var i=0;i<=bullets.length;i++){
this.ctx.drawImage(bullet,bullets[i][0]+55,bullets[i][1]); 这里报错
}变为
for(var i=0;i<bullets.length;i++){
this.ctx.drawImage(bullet,bullets[i][0]+55,bullets[i][1]); 这里报错
}
扩展资料:
Javascript被归类为直译语言,因为主流的引擎都是每次运行时加载代码并解译。V8是将所有代码解译后再开始运行,其他引擎则是逐行解译(SpiderMonkey会将解译过的指令暂存,以提高性能,称为实时编译),但由于V8的核心部分多数用Javascript撰写(而SpiderMonkey是用C++),因此在不同的测试上,两者性能互有优劣。
与其相对应的是编译语言,例如C语言,以编译语言编写的程序在运行之前,必须经过编译,将代码编译为机器码,再加以运行。
参考资料来源:百度百科-javascript
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询