为什么
var str = xmlHttp.responseText;alert(str);-----结果是 "yes" alert("yes"==str)----是false全部代码:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>异步提交</title>
<script type="text/javascript">
function test(name){
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET","as.jsp?name="+name,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){ var str = xmlHttp.responseText; if("yes"==str){
document.getElementById("testAjax").innerHTML="<h2>可以用哦</h2>";
}else{
document.getElementById("testAjax").innerHTML="<h2><font color='red'>用户名已经存在</font></h2>";
}
}
}
}
xmlHttp.send(null);
}
</script></head>
<body>
<input type="button" value="Ajax" onclick="test('aaa');"/>
<div id="testAjax"></div>
</body>
</html>
jsp==<% if(request.getParameter("name").equals("aaa")){
out.write("yes");
}
%>
var str = xmlHttp.responseText;alert(str);-----结果是 "yes" alert("yes"==str)----是false全部代码:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>异步提交</title>
<script type="text/javascript">
function test(name){
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET","as.jsp?name="+name,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){ var str = xmlHttp.responseText; if("yes"==str){
document.getElementById("testAjax").innerHTML="<h2>可以用哦</h2>";
}else{
document.getElementById("testAjax").innerHTML="<h2><font color='red'>用户名已经存在</font></h2>";
}
}
}
}
xmlHttp.send(null);
}
</script></head>
<body>
<input type="button" value="Ajax" onclick="test('aaa');"/>
<div id="testAjax"></div>
</body>
</html>
jsp==<% if(request.getParameter("name").equals("aaa")){
out.write("yes");
}
%>
在jsp里面用out.clear();然后再试试。
看字面不就知道了