java ajax中,关于 Servlet 标签问题
页面<scripttype="text/javascript">varxmlHttp;functioncreateXMLHttpRequest(){if(window.A...
页面
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function validate(){
createXMLHttpRequest();
var username = document.getElementById("username");
var url ="ValidationServlet?username="+ escape(username.value);
xmlHttp.open("get",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4 && xmlHttp.status ==200){
var message = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
var messageArea = document.getElementById("results");
messageArea.innerHTML ="<h4>" + message +"</h4>";
}
}
</script>
</head>
<body>
用户名:<input type= "text" size = "10" id= "username">
<input type = "button" value ="检测" onclick ="validate();">
<div id = "results"></div>
</body>
</html>
后台内容
public class ValidationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
String username= req.getParameter("username");
String message = "The name has not been used";
PrintWriter out = resp.getWriter();
if(username.equals("hacker")){
message ="The name has been used";
}
out.println("<response>");
out.println("<message>"+message+"</message>");
out.println("</response>");
上面的标签什么地方决定的,怎么换成别的,如何应用呀,谢谢亲们
}
} 展开
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function validate(){
createXMLHttpRequest();
var username = document.getElementById("username");
var url ="ValidationServlet?username="+ escape(username.value);
xmlHttp.open("get",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4 && xmlHttp.status ==200){
var message = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
var messageArea = document.getElementById("results");
messageArea.innerHTML ="<h4>" + message +"</h4>";
}
}
</script>
</head>
<body>
用户名:<input type= "text" size = "10" id= "username">
<input type = "button" value ="检测" onclick ="validate();">
<div id = "results"></div>
</body>
</html>
后台内容
public class ValidationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
String username= req.getParameter("username");
String message = "The name has not been used";
PrintWriter out = resp.getWriter();
if(username.equals("hacker")){
message ="The name has been used";
}
out.println("<response>");
out.println("<message>"+message+"</message>");
out.println("</response>");
上面的标签什么地方决定的,怎么换成别的,如何应用呀,谢谢亲们
}
} 展开
2个回答
展开全部
你说的是<response><message>之类的吗?这个只是xml标签,并不是有什么规定,是你代码里自己规定的(也可以随便写,比如<a><b>),你看看handleStateChange里,它读的时候读了message,意思是它认为后台肯定会发回<message>的内容,这是你代码里自己约定的
更多追问追答
追问
谢谢你,代码是我在网上复制的,如果,我要是,把response之类的,给替换了,运行,就不显示了。。。
追答
你是整体替换的吧。。。。无意中把responseXML里的这个也给替换掉了吧。。。
呵呵,你如果只替换标签的名字是不会有这个问题的,别直接替换,用手敲敲看,学代码可不能这么马虎
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询