Ajax的GET,POST方法传输数据和接收返回数据
//首先创建一个Ajax对象 function ajaxFunction(){ var xmlHttp; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e){ try{// Internet Explorer xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){} } } return xmlHttp; } --------------------- GET方法 function get() { //获取对象 var request = ajaxFunction(); //传输数据到你的某个Servlet request.open("GET" ,"/myAjax/Demo01?name=czc&age=22" ,true); //接收返回的数据,只发送可以不写 request.onreadystatechange=function(){ if(request.readyState==4 && request.status==200){ alert(request.responseText); } } //发送数据 request.send(); } --------------------- POST方法 function post(){ //获取对象 var request = ajaxFunction(); //发送的地址 request.open("POST" ,"/myAjax/Demo01" ,true); //接收返回的数据 request.onreadystatechange=function() { if (request.readyState==4 && request.status==200) { alert(request.responseText); } } //设置头 request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //发送数据的内容 request.send("name=cz&age=22"); } ---------------------