aJax get和post

<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>aJaxRequest!!!</title>
        <script type="text/javascript">
        function ajaxReq(){
            //获取用户请求数据
            var uname=document.getElementById("uname").value;
            var data="uname="+uname;
            //创建ajax引擎对象
            var ajax;
            if(window.XMLHttpRequest){
                ajax=new XMLHttpRequest();
            }else if(window.ActiveXObject){
                ajax=new ActiveXObject("Msxml2.XMLHTTP");
            }
            //复写onreadystatechange函数
            ajax.onreadystatechange=function(){
                //判断ajax状态码
                if(ajax.readyState==4){
                    //判断响应状态码
                    if(ajax.status==200){
                        //获取响应内容
                        var result=ajax.responseText;
                        //处理响应内容
                        alert(result);
                    }    
                }    
            }    
            //get方法
            //ajax.open("get","ajaxServlet",true);
            //ajax.send(null);
            //pos方法
            ajax.open("post","ajaxServlet",true);
            ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            ajax.send(data);
            }
        </script>
        <style type="text/css">
            #myDiv{
                border:solid 4px;
                width:100px;
                height:100px;
            }
        
        </style>
    </head>
    <body>
        <h3>aJax!!</h3>
        <input id="uname" type="text" name="uname" value="">
        <button id="myButton" onclick="ajaxReq()" />show!!!</button>
        <div id="myDiv"></div>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/lastingjava/p/9893164.html