9.SpringMVCA:JAX技术

9.SpringMVC:AJAX技术

9.1 什么是Ajax技术?

  • Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML
  • 是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部分网页的技术。
  • 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。

9.2 伪造ajax

  • 我们可以使用前端的一个标签来伪造一个ajax的样子。iframe标签
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>iframe体验页面无刷新</title>
</head>

    <script>
        function go() {
            var url=document.getElementById("url").value;
            document.getElementById("iframe").src=url;
        }
    </script>

<body>

<p>请输入地址</p>
<p>
    <input type="text" id="url" value="https://www.cnblogs.com/xuan-study">
    <input type="button"value="提交" οnclick="go()">
</p>

<div>
    <iframe id="iframe" style="width: 100%;height: 500px"> </iframe>
</div>

</body>
</html>

9.3 jQuery.ajax

  • Ajax的核心是XMLHttpRequest对象(XHR)。XHR为向服务器发送请求和解析服务器响应提供了接口。能够以异步方式从服务器获取新数据。jQuery 提供多个与 AJAX 有关的方法。

  • Query.ajax(...)
          部分参数:
                url:请求地址
                type:请求方式,GET、POST(1.9.0之后用method)
            headers:请求头
                data:要发送的数据
        contentType:即将发送信息至服务器的内容编码类型(默认: "application/x-www-form-urlencoded; charset=UTF-8")
              async:是否异步
            timeout:设置请求超时时间(毫秒)
          beforeSend:发送请求前执行的函数(全局)
            complete:完成之后执行的回调函数(全局)
            success:成功之后执行的回调函数(全局)
              error:失败之后执行的回调函数(全局)
            accepts:通过请求头发送给服务器,告诉服务器当前客户端可接受的数据类型
            dataType:将服务器端返回的数据转换成指定类型
              "xml": 将服务器端返回的内容转换成xml格式
              "text": 将服务器端返回的内容转换成普通文本格式
              "html": 将服务器端返回的内容转换成普通文本格式,在插入DOM中时,如果包含JavaScript标签,则会尝试去执行。
            "script": 尝试将返回值当作JavaScript去执行,然后再将服务器端返回的内容转换成普通文本格式
              "json": 将服务器端返回的内容转换成相应的JavaScript对象
            "jsonp": JSONP 格式使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数
    
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>


    <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script>

    <script>

      function a() {
        $.post({
          url: "${pageContext.request.contextPath}/ajax1",
          data:{"name":$("#username").val()},
          success:function (data) {
            alert(data);
          }
      })
      }

    </script>

  </head>
  <body>
  <%--失去焦点,发起一个请求--%>
  用户名:<input type="text" id="username" οnblur="a()">
  </body>
</html>
@RequestMapping("/ajax1")
public void testAjax1(String name, ServletResponse response) throws IOException {
    if ("xuan".equals(name)){
        response.getWriter().print("true");
    }else {
        response.getWriter().print("false");
    }
}

9.4 springmvc实现ajax

​ 第一步:编写实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private Integer age;
    private String name;
    private String gender;
}

​ 第二步:编写controller接口

@RequestMapping("/ajax2")
public List<User> testAjax2()  {
    List<User> userList= new ArrayList<User>();
    userList.add(new User(1,"xuange","男"));
    userList.add(new User(2,"yingge","男"));
    userList.add(new User(3,"goujieshen","男"));
    return userList;
}

​ 第三步:编写jsp测试用ajax取数据

  • 格式差不多,就取数据需要拼接字符串
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {
                $.post(
                    {
                        url: "${pageContext.request.contextPath}/ajax2",
                        success: function (data) {
                            var html="";
                            for (let i=0;i<data.length;i++){
                                html+= "<tr>" +
                                    "<td>"+data[i].age+"</td>"+
                                    "<td>"+data[i].name+"</td>"+
                                    "<td>"+data[i].gender+"</td>"+
                                "</tr>"
                            }
                            $("#content").html(html);
                        }
                    }
                )
            })
        })


    </script>
</head>
<body>


<%--加载数据--%>
<input type="button" value="加载数据" id="btn">
<table>
    <tr>
        <td>年龄:</td>
        <td>姓名:</td>
        <td>性别:</td>
    </tr>
    <tbody id="content">

    </tbody>
</table>
</body>
</html>

​ 第四步;页面展示

[
    {
        age: 1,
        name: "xuange",
        gender: "男"
    },
    {
        age: 2,
        name: "yingge",
        gender: "男"
    },
    {
        age: 3,
        name: "goujieshen",
        gender: "男"
    }
]

9.5 ajax注册提示效果

​ 第一步:编写controller接口

@RequestMapping("/ajax3")
public String testAjax3(String name,String password)  {
    String msg="";
    if (name!=null){
        if ("admin".equals(name)){
            msg="ok";
        }else {
            msg="用户名有误";
    }
    } if (password!=null){
        if ("123456".equals(password)){
            msg="ok";
        }else{
            msg="密码有误";
    }
    }
    return msg;
}

​ 第二步:编写jsp测试

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script>

    <script>
        function a() {
            $.post({
                url: "${pageContext.request.contextPath}/ajax3",
                data:{"name":$("#name").val()},
                success: function (data) {
                    if (data.toString()==='ok'){
                        $("#userInfo").css("color","green");
                    }else {
                        $("#userInfo").css("color","red");
                    }
                    $("#userInfo").html(data);
                }
            })
        }

        function b() {
            $.post({
                url: "${pageContext.request.contextPath}/ajax3",
                data:{"password":$("#password").val()},
                success: function (data) {
                    if (data.toString()==='ok'){
                        $("#passwordInfo").css("color","green");
                    }else {
                        $("#passwordInfo").css("color","red");
                    }
                    $("#passwordInfo").html(data);
                }
            })
        }
    </script>
</head>
<body>

    <p>
        用户名:<input type="text" id="name" οnblur="a()">
        <span id="userInfo"></span>
    </p>
    <p>
        密码:<input type="password" id="password" οnblur="b()">
        <span id="passwordInfo"></span>
    </p>


</body>
</html>

​ 第三步:测试结果

9.SpringMVC:AJAX技术

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值