为什么responseText返回了整个页面
如题:HTML代码<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>...
如题:
HTML代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script language="javascript" >
var http_request=false;
function userCheck(){
var username=document.getElementById("input_1").value;
if(username==""){
alert("用户名不能为空");
return false;
}
else{
send_request("111.jsp?username=",username);
}
}
function send_request(url,name){
var newURL=url+name;
alert(newURL);
if(window.XMLHttpRequest){
http_request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
try{
http_request=new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
http_request.onreadystatechange=processRequest;
http_request.open("GET",newURL,true);
http_request.send(null);
}
function processRequest(){
if(http_request.readyState==4){
if(http_request.status==200){
alert(http_request.responseText);
}else{
alert("您所请求的页面有异常");
}
}
}
</script>
</head>
<body>
用户名:<input type="text" id="input_1" name="input01">
<br>
<input type="button" id="input_2" name="input02" value="用户名校验" onclick="userCheck()">
</body>
</html>
JSP代码
<body>
<%
String username=request.getParameter("username");
if("raul".equals(username)) out.print("用户名已经被注册");
else out.print("用户名尚未被注册,您可以继续使用");
%>
</body>
我在JSP中加上了 response.setContentType("text/html"); 但alert中返回的还是整个页面,请高手帮忙调一下上面的代码 展开
HTML代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script language="javascript" >
var http_request=false;
function userCheck(){
var username=document.getElementById("input_1").value;
if(username==""){
alert("用户名不能为空");
return false;
}
else{
send_request("111.jsp?username=",username);
}
}
function send_request(url,name){
var newURL=url+name;
alert(newURL);
if(window.XMLHttpRequest){
http_request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
try{
http_request=new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
http_request.onreadystatechange=processRequest;
http_request.open("GET",newURL,true);
http_request.send(null);
}
function processRequest(){
if(http_request.readyState==4){
if(http_request.status==200){
alert(http_request.responseText);
}else{
alert("您所请求的页面有异常");
}
}
}
</script>
</head>
<body>
用户名:<input type="text" id="input_1" name="input01">
<br>
<input type="button" id="input_2" name="input02" value="用户名校验" onclick="userCheck()">
</body>
</html>
JSP代码
<body>
<%
String username=request.getParameter("username");
if("raul".equals(username)) out.print("用户名已经被注册");
else out.print("用户名尚未被注册,您可以继续使用");
%>
</body>
我在JSP中加上了 response.setContentType("text/html"); 但alert中返回的还是整个页面,请高手帮忙调一下上面的代码 展开
1个回答
展开全部
你的response 要set 它的contextType 为html/text 否则返回的就是带html 代码的数据
另,你的JSP输出不对你写的代码,out.print 最终会编译为servlet其输出的也是带html代码的,应该修改为:
PrintWriter pw=response.getWriter();
response.setContentType("html/text");
pw.write("你要输出的信息放这");
另,你的JSP输出不对你写的代码,out.print 最终会编译为servlet其输出的也是带html代码的,应该修改为:
PrintWriter pw=response.getWriter();
response.setContentType("html/text");
pw.write("你要输出的信息放这");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询