AJAX原生JavaScript写法

GET方式

 1                 //创建XMLHttpRequest对象,为考虑兼容性问题,老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象
 2                 var ajax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 3     
 4                 //设定请求的类型,服务器URL,以及是否异步处理
 5                 ajax.open("get","test.ashx?name=jcx&id="+new Date(),true);
 6                 
 7                 ajax.onreadystatechange=function()
 8                 {
 9                     //4:请求已完成,且响应已就绪    
10                     if(ajax.readyState==4)
11                     {
12                         //200:成功
13                         if(ajax.status==200)
14                         {
15                             //处理结果
16                             alert(ajax.responseText);
17                         }else
18                         {
19                         alert("AJAX服务器返回错误!");
20                         }
21                     }
22                 
23                 }
24 
25                 //将请求发送到服务器
26                 ajax.send();

POST方式

 1    var ajax=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
 2 
 3             ajax.open("post", "test.ashx", true);
 4 
 5             ajax.onreadystatechange = function () {
 6                 if  (ajax.readyState==4)
 7                 {
 8                     if (ajax.status==200) {
 9                         alert(ajax.responseText);
10                     }
11                 }
12             }
13 
14             ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
15             ajax.send("name=jcx&id=23");
原文地址:https://www.cnblogs.com/green-jcx/p/ajax.html