我试图在页面加载后使用 ajax 调用填充 html 选择。出于某种原因,没有调用 servlet 帖子。有谁看到我可能做错了什么?
window.onload = function(){
var localXMLHttp = getXMLHttpRequest();
localXMLHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
localXMLHttp.open("POST","DateServlet",true);
localXMLHttp.onreadystatechange = function() {
if(localXMLHttp.readyState == 4 && localXMLHttp.status == 200) {
alert(localXMLHttp.responseText);
var datesToDisplay = localXMLHttp.responseText.split("*");
var periodSelect = document.getElementById("period");
periodSelect.options[periodSelect.options.length] = new Option(datesToDisplay[0], datesToDisplay[0]);
periodSelect.options[periodSelect.options.length] = new Option(datesToDisplay[1], datesToDisplay[1]);
periodSelect.options[periodSelect.options.length] = new Option(datesToDisplay[2], datesToDisplay[2]);
periodSelect.options[periodSelect.options.length] = new Option(datesToDisplay[3], datesToDisplay[3]);
}
};
localXMLHttp.send(null);
};
此代码发布在正文标记末尾的脚本标记内。