什么是ajax,作用是什么?

AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。

Ajax 不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的Web应用程序的技术。

jQuery 提供多个与 AJAX 有关的方法。

通过 jQuery AJAX 方法,您能够使用 HTTP Get 和 HTTP Post 从远程服务器上请求文本、HTML、XML 或 JSON – 同时您能够把这些外部数据直接载入网页的被选元素中。

jQuery 不是生产者,而是大自然搬运工。

jQuery Ajax本质就是 XMLHttpRequest,对他进行了封装,方便调用!

利用jQuery使用ajax

在项目中导入jQuery.js


配置静态资源过滤,不然js加载不到页面

<%--
  Created by IntelliJ IDEA.

  Date: 2020/9/23
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
  <head>
   <base href="<%=basePath %>"/>
    <title>$Title$</title>



      <script src="statics/js/jquery-1.11.3.js"></script>
      <script>
          function a1(){
   

              $.ajax({
   
                  type: "post",
                  url:"${pageContext.request.contextPath}/book/a1",
                  data:{
   'name':$("#txtName").val()},
                  success:function (data,status) {
   
                      alert(data);
                      alert(status);
                  }
              });
          }
      </script>
  </head>
  <body>

  <hr>
  <%--onblur:失去焦点触发事件--%>
  用户名:<input type="text" id="txtName" onblur="a1()"/>

  </body>
</html>

 @RequestMapping("/a1")
    public void ajax1(String name , HttpServletResponse response) throws IOException {
   
        if ("admin".equals(name)){
   
            response.getWriter().print("true");
        }else{
   
            response.getWriter().print("false");
        }
    }