javascript原生ajax;

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <script>
 9         //a、new对象;
10     var xmlhttpRequest =null;
11     function ajaxRequest(){
12         if(window.ActiveXObject){
13             xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
14         }else if(Window.XMLHttpRequest){
15             xmlhttpRequest = new XMLHttpRequest();
16         };
17         if(null != xmlhttpRequest){
18             var v1 = 1,v2 = 2;
19             //AjaxServlet其实是url,ajax数据存放的地址;
20             //b.连接服务器吧;
21             xmlhttpRequest.open("get","AjaxServlet?v1="+v1+"&v2="+v2,true);
22             xmlhttpRequest.open("POST","AjaxServlet",true);
23             //d、处理回调
24             xmlhttpRequest.onreadystatechange = ajaxCallBack;
25             //post必须加下面这句,get时去掉;
26             //c、设置响应头,看情况,上去
27             xmlhttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
28             //d、数据发送
29             xmlhttpRequest.send("v1="+v1+"&v2="+v2);
30         }
31     }
32     function ajaxCallBack(){
33         if(xmlhttpRequest.readyState == 4){
34             if(xmlhttpRequest.status == 200){
35                 var content = xmlhttpRequest.responseText;
36                 console.log(content);
37             }
38         }
39     }
40 </script>
41 </body>
42 </html>

 

posted @ 2015-11-04 18:17  挥刀  阅读(147)  评论(0编辑  收藏  举报