ajax基础实例 返回xml
时间:2011-02-23 来源:岁月无痕有影
xmlhttp_request.setRequestHeader("Content-Type", "text/xml"); //setRequestHeader("header", "value")
xmlhttp_request.setRequestHeader("Charset", "gb2312"); //把指定首部设置为所提供的值。在设置任何首部之前必须先调用open()
xmlhttp_request.send(null);
var self = xmlhttp_request; //把引用赋给临时变量self
xmlhttp_request.onreadystatechange = function() {
thecallback(self);
};
}
//ajax的回调函数
function thecallback(xmlhttp_request) {
if (xmlhttp_request.readyState == 4 && xmlhttp_request.status == 200) {
//绑定项
var data = xmlhttp_request.responseXML;
var citys = data.getElementsByTagName("name");
var cns = data.getElementsByTagName("code");
var listCity = $("listCity");
listCity.innerHTML = ""; //先移除所有的项
var listP = $("listProvince");
var code = listP.options[listP.selectedIndex].value;
var item = new Array();
for (var i = 0; i < citys.length; i++) {
if (citys.length != 1 && code == cns[i].childNodes[0].data) continue;
item[i] = document.createElement("option");
item[i].value = cns[i].childNodes[0].data;
item[i].innerHTML = citys[i].childNodes[0].data;
listCity.appendChild(item[i]);
}
//绑定项结束
$("txtCode").value = listCity.options[0].value;
}
}