ajax之post请求方式

来源:互联网 发布:阿里云网站更改负责人 编辑:程序博客网 时间:2024/06/04 19:50
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script>
        function f1(){
            var username=document.getElementById('username').value;
           
       //对传递的特殊的特殊符号进行编码处理     这步必须放到请求字符串之前
        username=encodeURIComponent(username);


         //把用户名信息变成“请求字符串”
            var info="name="+username+"&age=23";



        //1.创建ajax对象
        if(typeof ActiveXObject!="undefined")
        {
            var xhr=new ActiveXObject("Microsoft.XMLHTTP");//最原始方式
            var xhr=new ActiveXObject("Msxml2.XMLHTTP");//升级
             var xhr=new ActiveXObject("Msxml2.XMLHTTP.3.0");//升级
              var xhr=new ActiveXObject("Msxml2.XMLHTTP.6.0");//升级
        }
        else
        {
            var xhr=new XMLHttpRequest(); 
        }
        //设置事件
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4)
            {
               alert(xhr.responseText);
            }
        }
            //连接服务器
            xhr.open('post','3.php',true);//异步传输:同一时间执行多个进程


            //post方式传递数据是模拟form表单传递数据
            //form表单的post格式数据是通过xml形式传递给服务器的
          //该方法必须要在open()方法后调用
            xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");




            //发送请求
            xhr.send(info);    //post方式请求需要把信息组织为请求字符串传递给send()方法
         }


        </script>
    </head>
    <body>                                                     /*失去焦点触发*/
   用户名: <input type="text" id="username" name="username" onblur="f1()" />
    <input type="password" id="pws"  name="password"/>
  
    </body>

</html>


//3.php

<?php
print_r($_POST);
?>

0 0
原创粉丝点击