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 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询