• 使用$.ajax方式实现页面异步访问,局部更新的效果


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
    function fun() {
    $.ajax({
    url:"ajaxServlet",
    type:"POST",
    data:{
    "username":"light",
    "age":12
    },
    success:function (data) {
    alert(data);
    },
    error:function () {
    alert("出错啦");
    },
    dataType:"text"
    });
    }

    });
    }

    </script>
    </head>
    <body>
    <input type="button" value="发送异步请求" onclick="fun();">
    <input type="text">
    </body>
    </html>

    package cn.hopetesting.com;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author newcityman
    * @date 2019/9/16 - 21:53
    */
    @WebServlet("/ajaxServlet")
    public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(username);
    response.getWriter().write("hello,"+username+".");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
    }
    }
     
  • 相关阅读:
    python 使用else代替状态变量
    python 实现线程安全的单例模式
    sql语句的执行顺序
    python 实现int函数
    python实现时间o(1)的最小栈
    python实现简单的负载均衡
    python实现求最长回文子串长度
    python lambda表达式
    sql针对某一字段去重,并且保留其他字段
    基本认识
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11530830.html
Copyright © 2020-2023  润新知