AJAX有另外一个名字叫XML HTTP请求,是微软首先创建的。
get和post方式对比
Get方式 | Post方式 |
function doRequestUsingGET(){ //1、创建XMLHttpRequest对象 if(window.ActiveXObject){ //IE5、IE6浏览器 var xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); }else if(window.XMLHttpRequest){ //其他浏览器 var xmlHttp = new XMLHttpRequest(); } //2、设置回调函数 xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200){ var responseDiv = document.getElementById(serverResponse); responseDiv.innerHTML = decodeURI(xmlHttp.responseText); } } //3、创建连接
var queryString = “name1=value1&name2=value2”; var url = “test.php” var data = url+”?”+queryString; xmlHttp.open("GET",data);
//4、发送数据 xmlHttp.send(null); }
| function doRequestUsingPOST(){ //1、创建XMLHttpRequest对象 if(window.ActiveXObject){ //IE5、IE6浏览器 var xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); }else if(window.XMLHttpRequest){ //其他浏览器 var xmlHttp = new XMLHttpRequest(); } //2、设置回调函数 xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200){ var responseDiv = document.getElementById(serverResponse); responseDiv.innerHTML = decodeURI(xmlHttp.responseText); } } //3、创建连接
var queryString = “name1=value1&name2=value2”; var url = “test.php” xmlHttp.open("POST",url); //4、设置HTTP请求头部 xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”) //5、发送数据
xmlHttp.send(queryString); }
|