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个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询