jQuery的ajax方法

jQuery的ajax方法

ajxa方法
url 表示请求的地址
type 表示请求的类型
data 表示发送服务器的数据
格式有两种:
一、name=value&name=value
二、{key:value}
success 请求成功,响应的回调函数
dataType 响应的数据 常用的数据类型有text 表示纯文本、xml 表示xml数据、json 表示json数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta  http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
      $(function (){
          // ajax请求
          $("#ajaxBtn").click(function(){
              $.ajax({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  //data:"action=jqueryAjax",
                  data:{action:jqueryAjax},
                  type:"GET",
                  success:function (data){
                    //alert("服务器返回的数据类型:"+data);
                    //var jsonObj=JSON.parse(data);

                    $("#msg").html("姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType:"json"
              });
          });
      });
    </script>
    <title>Title</title>
</head>
<body>
<div>
    <button id="ajaxBtn">$.ajax请求</button>
    <button id="getBtn">$.get请求</button>
    <button id="postBtn">$.post请求</button>
    <button id="getJsonBtn">$.getJson请求</button>
</div>
<div id="msg">

</div>
</body>
</html>


这里引入js包是通过

 <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>

$.get方法

$.post方法

url 带载入页面的url地址
data 待发送key/value参数
callback 载入成功回调函数
type 返回内容格式,xml,html,script,json,text_default

<!DOCTYPE html>
<html lang="en">
<head>
    <meta  http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
      $(function (){
          // ajax请求
          $("#ajaxBtn").click(function(){
              $.ajax({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  //data:"action=jqueryAjax",
                  data:{action:jqueryAjax},
                  type:"GET",
                  success:function (data){
                    //alert("服务器返回的数据类型:"+data);
                    //var jsonObj=JSON.parse(data);

                    $("#msg").html("姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType:"json"
              });
          });
          $("#getBtn").click(function (){
              $.get({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  data:"action=jqueryGet",
                  success:function(data){
                      $("#msg").html("get 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType: "json"
              });
          });

          $("#postBtn").click(function (){
              $.post({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  data:"action=jqueryPost",
                  success:function(data){
                      $("#msg").html("post 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType: "json"
              });
          });
      });
    </script>
    <title>Title</title>
</head>
<body>
<div>
    <button id="ajaxBtn">$.ajax请求</button>
    <button id="getBtn">$.get请求</button>
    <button id="postBtn">$.post请求</button>
    <button id="getJsonBtn">$.getJson请求</button>
</div>
<div id="msg">

</div>
</body>
</html>

ajax——jquery 的getJSON方法

概述:通过HTTP GET请求载入JSON数据。
url 请求的url地址
data 发送给服务器的数据
callback 成功的回调函数

 $("#getJsonBtn").click(function (){
              $.getJSON("http://localhost:8080/JavaWEB/ajax","action=jqueryGetJSON",function (data){
                  $("#msg").html("getJSON 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex)
              });
          });


表单序列化serialize()

serialize()可以把表单中所有表单项的内容都获取到,并以name=value&name=value的形式进行拼接

<!DOCTYPE html>
<html lang="en">
<head>
    <meta  http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
      $(function (){
          // ajax请求
          $("#ajaxBtn").click(function(){
              $.ajax({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  //data:"action=jqueryAjax",
                  data:{action:jqueryAjax},
                  type:"GET",
                  success:function (data){
                    //alert("服务器返回的数据类型:"+data);
                    //var jsonObj=JSON.parse(data);

                    $("#msg").html("姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType:"json"
              });
          });
          $("#getBtn").click(function (){
              $.get({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  data:"action=jqueryGet",
                  success:function(data){
                      $("#msg").html("get 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType: "json"
              });
          });

          $("#postBtn").click(function (){
              $.post({
                  url:"http://localhost:8080/JavaWEB/ajax",
                  data:"action=jqueryPost",
                  success:function(data){
                      $("#msg").html("post 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex);
                  },
                  dataType: "json"
              });
          });

          $("#getJsonBtn").click(function (){
              $.getJSON("http://localhost:8080/JavaWEB/ajax","action=jqueryGetJSON",function (data){
                  $("#msg").html("getJSON 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex)
              });
          });

          $("#submit").click(function (){
              //把参数序列化
              alert($("#form01").serialize());
              $.getJSON("http://localhost:8080/JavaWEB/ajax","action=jquerySerialize&"+$("#form01").serialize(),function (data){
                  $("#msg").html("jquerySerialize 姓名:"+data.name+",年龄:"+data.age+",性别:"+data.sex)
              });
          });

      });
    </script>
    <title>Title</title>
</head>
<body>
<div>
    <button id="ajaxBtn">$.ajax请求</button>
    <button id="getBtn">$.get请求</button>
    <button id="postBtn">$.post请求</button>
    <button id="getJsonBtn">$.getJson请求</button>
</div>
<div id="msg">

</div>


<br><br>

<form id="form01">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"> <br>
    下拉单选:
    <select name="single">
        <option value="single1">single1</option>
        <option value="single2">single2</option>
    </select>
    <br>
    下拉多选:
    <select name="multiple" multiple="multiple">
        <option value="Multiple1" selected="selected">Multiple1</option>
        <option value="Multiple2">Multiple2</option>
        <option value="Multiple3" selected="selected">Multiple3</option>
    </select>
    <br>
    复选:
    <input type="checkbox" name="check" value="check1">check1
    <input type="checkbox" name="check" value="check2" checked="checked">check2 <br>
    单选:
    <input type="radio" name="radio" value="radio1" checked="checked">radio1
    <input type="radio" name="radio" value="radio2">radio2 <br>

</form>
<button id="submit">提交-seriliaze</button>

</body>
</html>

服务端servlet程序接收:

   protected void jquerySerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("jquerySerialize  请求过来了");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        Student student = new Student("张三", 25, "男");

        Gson gson = new Gson();

        String s = gson.toJson(student);

        resp.getWriter().write(s);

    }
    ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为大牛的渣渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值