AJAX与Servlet的get与post数据交互

7 篇文章 1 订阅
4 篇文章 0 订阅

浏览器向服务器发送get请求

浏览器从后台获取数据

JavaScript部分

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            
        </style>
    </head>
    <body>
        <button id="btn">按钮</button>
    </body>
    <script src="js/jquery-3.3.1.js"></script>
    <script>
        $('#btn').click(function(){
            $.ajax({
                type:'GET',
                url:'ajaxTest',
                dataType:'json',
                success:function(resp){
                    showResult(resp);
                }
            });
        });
        
        function showResult(result){
            for(var stu of result){
                console.log(stu.name);
                console.log(stu.id);
                console.log(stu.sex);
                console.log(stu.phone);
            }
        }
    </script>
</html>

 Servlet部分

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

        LinkedList<Student> students = new LinkedList<Student>();
        students.add(new Student(2001,"Tom",true,"18438825932"));
        
        JSONArray result = new JSONArray(students);
        
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        Writer writer = response.getWriter();
        writer.write(result.toString(2));
        writer.close();
    }

浏览器向服务器发送post请求

浏览器向后台传递数据

JavaScript部分

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            
        </style>
    </head>
    <body>
        <input type="text" />
        <input type="password" />
        <button id="btn">按钮</button>
    </body>
    <script src="js/jquery-3.3.1.js"></script>
    <script>
        $('#btn').click(function(){
            var id = $('input').eq(0).val();
            var password = $('input').eq(1).val();
            
            $.ajax({
                type:'POST',
                url:'postTest',
                data:{
                    method:'login',
                    id:id,
                    password:password
                },
                dataType:'json',
                success:function(resp){
                    console.log("success");
                }
            });
        });
    </script>
</html>

 Servlet部分

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        
        System.out.println(id);
        System.out.println(password);
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值