日期:2014-05-16  浏览次数:20581 次

jsp ajax post的问题
xmlhttp.readyState一直是等于500 返回不了值
js代码
<script type="text/javascript">
function post()
{

var text;
 text = document.getElementById('textarea').value; //获取textarea的值
// document.getElementById("myDiv").innerHTML=text;
var xmlhttp;    
//if (str=="")
//  {
//  document.getElementById("txtHint").innerHTML="";
//  return;
//  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
  
   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  
    }
  else {
  document.getElementById("myDiv").innerHTML="status:"+xmlhttp.status;//xmlhttp.readyState;
  
   }
  }
xmlhttp.open("POST","post.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded charset=GBK");
xmlhttp.send("utext="+text);
}

post.jsp

<%
String content = new String(request.getParameter("utext").getBytes("ISO8859_1"), "GBK");  //获取表单输入的信息
out.println(content+"<br>");
%>


------解决方案--------------------
500就是你jsp页面有错误,直接浏览器访问jsp页面看是什么错误,修改jsp错误好了再用ajax请求

//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded charset=GBK");
//==>不用加charset
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

------解决方案--------------------
你没有解码就直接存数据库了。。要解码后再保存