================================首先把它写到<script>标签里========================================
if (window.ActiveXObject && !window.XMLHttpRequest) { window.XMLHttpRequest = function() { var msxmls = ['Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP']; for (var i = 0; i < msxmls.length; i++) { try { return new ActiveXObject(msxmls[i]); } catch (e) {} } return null; }; }
var http = new XMLHttpRequest();
===点击事件,发送请求===
function sendRequest() {
//编码 是传进来的参数 //data = encodeURIComponent(d);
//后台转码:String d = request.getParameter("d"); //d = new String(d.getBytes("iso-8859-1"),"UTF-8"); var d = new Date(); //确定请求的地址和请求的方式
http.open("GET","<%=path%>/servlet/Servlet2?" + d.getTime(), true); //设置回调函数 http.onreadystatechange=getRespnse; //如果是GET请求,请求数据为null http.send(null); }
、、、、、、、、如果是POST请求
function sendRequest(d){ d = encodeURIComponent(d); http.open("post", '<%=path%>/servlet/ajax3', true); //设置请求头 http.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http.onreadystatechange=getResponse; http.send("d=" + d);//这里往过传数据 }
====回应===============
function getRespnse(){ if(http.readyState==4){ var div = document.getElementByIdx_x("date"); var s = http.responseText; div.innerHTML = s; }}
Servlet通过out.print("XXX");把值传过来,JSP页面用http.responseText来接受!