Java第四十二天,Ajax

一、概念

ASynchronous JavaScript And XML,异步的 JavaScript  和 XML

通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新传统的网页(不使用Aax)如果需要更新内容,必须重载整个网页页面

二、同步和异步

1.同步

客户端必须等待服务器端的响应;在等待的期间,客户端不能做其它操作

2.异步

客户端不需要等待服务器端的响应;在服务器处理请求的过程中,客户端可以去进行其它的操作

3.同步和异步图解

三、原生 Ajax 使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function  fun() {
            let xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            //2. 建立连接
            /*
                参数:
                    1. 请求方式:GET、POST
                        * get方式,请求参数在URL后边拼接。send方法为空参
                        * post方式,请求参数在send方法中定义
                    2. 请求的URL:
                    3. 同步或异步请求:true(异步)或 false(同步)

             */
            xmlhttp.open("GET","first?username=admin",true);

            //3.发送请求
            xmlhttp.send();

            //4.接受并处理来自服务器的响应结果
            //获取方式 :xmlhttp.responseText
            //什么时候获取?当服务器响应成功后再获取

            //当xmlhttp对象的就绪状态改变时,触发事件onreadystatechange。
            xmlhttp.onreadystatechange=function()
            {
                //判断readyState就绪状态是否为4,判断status响应状态码是否为200
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    //获取服务器的响应结果
                    let responseText = xmlhttp.responseText;
                    alert(responseText);
                }
            }

        }
    </script>
</head>
<body>
<button onclick="fun();">发送请求</button>
</body>
</html>

四、JQeury 实现 Ajax

1.$.ajax()

2.$.get():发送get请求

语法:$.get(url, [data], [callback], [type])

参数:

url:请求路径

data:请求参数

callback:回调函数

type:响应结果的类型

3.$.post():发送post请求

语法:$.post(url, [data], [callback], [type])

参数:

url:请求路径

data:请求参数

callback:回调函数

type:响应结果的类型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.js"></script>
    <script>
        function send1() {
            $.ajax({
                url:"first" , // 请求路径
                type:"POST" , //请求方式
                // data: "username=jack&age=20", //请求参数
                data:{"username":"admin","age":20},
                success:function (data) {
                    alert(data);
                },//响应成功后的回调函数
                error:function () {
                    alert("出错啦...")
                },//表示如果请求响应出现错误,会执行的回调函数
                dataType:"text"//设置接受到的响应数据的格式
            });
        }
        function send2() {
            $.get("first", {"username":"admin","age":20},
                function (data) {
                    alert(data)
                }
                , "text")
        }
        function send3() {
            $.post("first", {"username":"admin","age":20},
                function (data) {
                    alert(data)
                }, "text")
        }
    </script>
</head>
<body>
<button onclick="send1()">发送请求1</button>
<button onclick="send2()">发送请求2</button>
<button onclick="send3()">发送请求3</button>
</body>
</html>
原文地址:https://www.cnblogs.com/viplanyue/p/13573673.html