Javaservlet+ajax+jsp
嗨,在使用Ajax调用servlet之后,我试图用一些文本填充jsp文件中声明的文本字段。xmlhttp。responseText包含servlet返回的文本,但是带有消息id的输入没有填充该文本,我不知道为什么。有什么建议吗?使用的代码如下
function ajaxFunction() {
if(xmlhttp) {
var txtname = document.getElementById("txtname");
xmlhttp.open("POST","getname",true); //getname will be the servlet name
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + txtname.value); //Posting txtname to Servlet
}
}
function handleServerResponse() {
//alert(xmlhttp.readyState);//ok
if (xmlhttp.readyState == 4) {
alert(xmlhttp.status);
if(xmlhttp.status == 200) {
alert(xmlhttp.responseText); // this is ok
document.getElementById("message").innerHTML=xmlhttp.responseText; // doesn't work
//document.myForm.message.innerHTML=xmlhttp.responseText; //neither
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>
<body>
<form name="myForm" method="POST" action="/ajjax/getname">
<table>
<tr>
<td>Enter Name</td>
<td><input type="text" name="txtname" id="txtname" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Submit" onclick="ajaxFunction();" /></td>
</tr>
</table>
<input type="text" name="message" id="message" />
</form>
</body>
</head> </html>
# 1 楼答案
试试这个:
<input/>
字段具有value
属性,innerHtml
将无法工作(至少如预期的那样)