AJAX学习:
JSP: function ajax(){
var xmlhttp;
if(window.XMLHttpRequest){ //创建XMLHTTPrequest对象:ie5,ie6用Microsoft.XMLHTTP
xmlhttp = new XMLHttpRequest();
}else{
ActiveObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","basicinformation_zgxx_ajaxTest.action", true);//以post方式提交,路径,是否为异步
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//post时:加HTTP头
xmlhttp.send("gh=132&id=2");//传值仅用于post,get直接在路径上写
xmlhttp.onreadystatechange=function(){//准备状态改变时
if(xmlhttp.readyState==4&&xmlhttp.status==200)//表示已经响应就绪
{
alert(xmlhttp.responseText);//得到后台传的值
}
}
}
后台:
public voidajaxTest(){
System.out.println("+++"+gh);
HttpServletResponsere =ServletActionContext.getResponse();
re.setCharacterEncoding("UTF-8");// 指定返回值的编码方式,必须放在out声明之前
re.setContentType("text/html;charset=UTF-8");
PrintWriterpw = null;
Stringstr = "ajax传输";
try {
pw= re.getWriter();
pw.append(str);
pw.flush();
}catch(IOException e) {
e.printStackTrace();
}finally{
pw.close();
}
}