
ajax中为什么xmlhttp.responseText返回值为空字符串
ajax中为什么xmlhttp.responseText返回值为空字符串??JS:window.onload=function(){document.getElement...
ajax中为什么xmlhttp.responseText返回值为空字符串??
JS:
window.onload = function(){
document.getElementById("ok").onclick = function(){
var xmlhttp = ajaxFunction();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
alert(xmlhttp.responseText);
}
}
}
xmlhttp.open("post","/servlet/AjaxTestServlet",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("username=aaa");
};
};
SERVLET:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println(username);
System.out.println(username);
} 展开
JS:
window.onload = function(){
document.getElementById("ok").onclick = function(){
var xmlhttp = ajaxFunction();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
alert(xmlhttp.responseText);
}
}
}
xmlhttp.open("post","/servlet/AjaxTestServlet",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("username=aaa");
};
};
SERVLET:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println(username);
System.out.println(username);
} 展开
2个回答
展开全部
请把控制台输出界面截图下来我看一下。我觉得你的Servlet有错误,至少response.setCharacterEncoding("text/html");就错了。我想看看你XMLHttpRequest的实例对象是否访问到了Servlet,如果访问到了,控制台应该会输出username的值。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询