jQuery AJAX

这篇博客介绍了jQuery中的AJAX技术,包括GET和POST请求的使用方法。通过示例代码展示了如何使用$.get()、$.post()及$.ajax()进行异步数据交互,以及服务器端的响应处理。示例中,点击按钮触发AJAX请求,更新页面显示的时间和服务器返回的数据。
摘要由CSDN通过智能技术生成
1、jQuery AJAX简介

在这里插入图片描述

2、get和post请求

在这里插入图片描述

3、$.ajax()

在这里插入图片描述

4、示例代码
get.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style type="text/css">
        #time,#res{
            width: 300px;
            height: 100px;
            border: 1px solid #ff0;
        }
    </style>

</head>
<body>
    <script src="jquery-3.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var time=new Date();
            $("#time").html(time.getHours()+":"+time.getMinutes()+":"+time.getSeconds());

            $("#btn").click(function () {
                $.get("get",function (data) {
                    $("#res").html(data);
                });
            });
        });
    </script>

    <button id="btn">get请求</button>
    <div id="time"></div>
    <div id="res"></div>

</body>
</html>
post.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style type="text/css">
        #time,#res{
            width: 300px;
            height: 100px;
            border: 1px solid #ff0;
        }
    </style>

</head>
<body>
    <script src="jquery-3.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var time=new Date();
            $("#time").html(time.getHours()+":"+time.getMinutes()+":"+time.getSeconds());

            $("#btn").click(function () {
                $.post("post",{name:'zs',age:25},function (data) {
                    $("#res").html(data);
                });
            });
        });
    </script>

    <button id="btn">post请求</button>
    <div id="time"></div>
    <div id="res"></div>

</body>
</html>
ajax.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style type="text/css">
        #time,#res{
            width: 300px;
            height: 100px;
            border: 1px solid #ff0;
        }
    </style>

</head>
<body>
    <script src="jquery-3.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var time=new Date();
            $("#time").html(time.getHours()+":"+time.getMinutes()+":"+time.getSeconds());

            $("#btn").click(function () {
                $.ajax(
                    {
                        url:"post",
                        data:{name:'lisi',age:'30'},
                        type:'post',
                        success:function (data) {
                            $("#res").html(data.res);
                        },
                        dataType:'json'
                    }
                )
            });
        });
    </script>

    <button id="btn">ajax请求</button>
    <div id="time"></div>
    <div id="res"></div>

</body>
</html>
GetTest.java
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;
import java.io.PrintWriter;

public class GetTest {
    @WebServlet(value = "/get")
    public class PostTest extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter printWriter=resp.getWriter();
            printWriter.write("hello ajax!!");
        }
    }

}

PostTest.java
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;
import java.io.PrintWriter;

@WebServlet(value = "/post")
public class PostTest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("name");
        String age=req.getParameter("age");
        String json="{\"res\":\"success\"}";
        PrintWriter printWriter=resp.getWriter();
        printWriter.write(json);

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值