嗨,我正在使用 servlet 处理我的第二个 ajax 程序。但它不起作用。请帮忙
index.html
<!DOCTYPE html>
<html>
<head>
<script>
function callServer(str){
alert(str);
var myxml = new XMLHttpRequest();
myxml.onreadystatechange = function {
if(myxml.readyState == 4 && myxml.status == 200)
document.getElementById("replay").innerHTML = myxml.responseText;
}
myxml.open("GET",""/suggest?query=str",true);
myxml.send();
}
</script>
</head>
<body>
<input id="enter" type="text" onkeyup="callServer(this.value)" />
<div id="replay"></div>
</body>
</html>
我的servlet就在这里...
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/suggest")
public class suggest extends HttpServlet {
public suggest() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String [] values = {"sha","kla","hello","bun"};
String query = request.getParameter("query");
for (int i = 0; i < values.length; i++) {
if (values[i].equals(query))
response.getWriter().write("OK its available dude... "+query);
else response.getWriter().write("Sorry not yet... "+query);
}
}
}
这里我认为错误可能在js函数callserver中。我已经用一个警告语句检查了每一行,它工作正常,直到行 myxml.onreadystatechane = function。警报一直有效。请帮忙