当前位置: 代码迷 >> Java Web开发 >> servler怎么接收前台ajax xmlhttp.send("&键=值&")
  详细解决方案

servler怎么接收前台ajax xmlhttp.send("&键=值&")

热度:886   发布时间:2016-04-17 01:33:09.0
servler如何接收前台ajax xmlhttp.send("&键=值&")
前台js代码:

var xmlhttp;
function creatxmlhttp(){
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlhttp=new XMLHttpRequest();
}
}
function classdy(){
creatxmlhttp();
var text=document.ajaxform.texts.value;
var pr="&bid="+text+"&";
xmlhttp.onreadystatechange=getClassName;
xmlhttp.open("post","servlet/ajaxServlet",true);
xmlhttp.setRequestHeader("Content-Type","text/xml");
xmlhttp.send(pr);
}
function getClassName(){
if(xmlhttp.readystate==4){
if(xmlhttp.status==200){
var a=xmlhttp.responseText;
document.getElementById("show").innerText=a;
}
if(xmlhttp.status==404){
alert("没有找到请求页");
}
}
}
后台servlet代码:
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String ss=request.getParameter("bid");
System.out.println(ss);
out.print(ss);
out.flush();
out.close();

可最后的结果是ss=null;

大虾们帮帮忙啊

------解决方案--------------------
既然
String ss=request.getParameter("bid"); 
System.out.println(ss); 
打印的是null
肯定是xmlhttp.open("post","servlet/ajaxServlet",true);或者web.xml有问题
function classdy(){ 
creatxmlhttp(); 
var text=document.ajaxform.texts.value; 
xmlhttp.onreadystatechange=getClassName; 
url="/项目名/servlet/ajaxServlet?texts="+text
xmlhttp.open("post",url,true); 
xmlhttp.setRequestHeader("Content-Type","text/xml"); 
xmlhttp.send(null); 
}
 String ss=request.getParameter("texts"); 
System.out.println(ss); 


------解决方案--------------------
var pr="bid="+text; 

xmlhttp.open("post","servlet/ajaxServlet",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Length",pr.length);
xmlhttp.setRequestHeader("Connection", "close");
  相关解决方案