千峰jQuery 【Ajax】

GET POST PUT

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../jQuery/jQuery.min.js"></script>
  </head>
  <body>
    <button id="myget">get</button>
    <button id="mypost">post</button>
    <button id="myput">put</button>
    <button id="mypatch">patch</button>
    <button id="mydel">delete</button>
  </body>
  <script>
    $("#myget").click(function () {
      $.ajax({
        url: "http://localhost:3000/users",
        data: {
          name: "xiaoming",
        },
        success: function (res) {
          console.log(res);
        },
        error: function (err) {
          console.log(err);
        },
      });
    });

    $("#mypost").click(function () {
      $.ajax({
        url: "http://localhost:3000/users",
        data: {
          username: "xiaoming",
          sex: "woman",
        },
        method: "POST",
        success: function (res) {
          console.log(res);
        },
        error: function (err) {
          console.log(err);
        },
      });
    });

    $("#myput").click(function () {
      $.ajax({
        url: "http://localhost:3000/users/4",
        data: {
          username: "xiaohong",
          sex: "woman",
        },
        method: "PUT",
        success: function (res) {
          console.log(res);
        },
        error: function (err) {
          console.log(err);
        },
      });
    });
  </script>
  <!-- 注意,在json文件中id值不用加双引号,否则数字就会变成字符串格式显示 -->
</html>

JSON:

{
  "users": [
    {
      "name": "xiaoming",
      "sex": "man",
      "id": 1
    },
    {
      "name": "xiaoming1",
      "sex": "man",
      "id": 2
    },
    {
      "name": "xiaoming2",
      "sex": "man",
      "id": 3
    },
    {
      "username": "xiaohong",
      "sex": "woman",
      "id": 4
    },
    {
      "username": "xiaoming",
      "sex": "woman",
      "id": "xao3uwU"
    },
    {
      "username": "xiaoming",
      "sex": "woman",
      "id": "SHmpbO8"
    },
    {
      "username": "xiaoming",
      "sex": "woman",
      "id": 5
    },
    {
      "username": "xiaoming",
      "sex": "woman",
      "id": 6
    },
    {
      "username": "xiaoming",
      "sex": "woman",
      "id": 7
    }
  ],
  "lists": [
    "111",
    "222",
    "333"
  ]
}

promise写法:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../jQuery/jQuery.min.js"></script>
  </head>
  <body>
    <button id="myget">get</button>
    <button id="mypost">post</button>
    <button id="myput">put</button>
    <button id="mypatch">patch</button>
    <button id="mydel">delete</button>
  </body>
  <script>
    $("#myget").click(function () {
      // $.ajax({
      //   url: "http://localhost:3000/users",
      //   data: {
      //     name: "xiaoming",
      //   },
      // })
      //   .then((res) => {
      //     console.log(res);
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });

      // 简易写法:
      $.get(
        "http://localhost:3000/users",
        {
          name: "xiaoming",
        }
        // function (res) {
        //   console.log(res);
        // }
      ).then((res) => {
        console.log(res);
      });
    });

    $("#mypost").click(async function () {
      var res = await $.ajax({
        url: "http://localhost:3000/users",
        data: {
          username: "xiaoming",
          sex: "woman",
        },
        method: "POST",
      });
      console.log(res);
    });

    $("#myput").click(function () {
      $.ajax({
        url: "http://localhost:3000/users/4",
        data: {
          username: "xiaohong",
          sex: "woman",
        },
        method: "PUT",
        success: function (res) {
          console.log(res);
        },
        error: function (err) {
          console.log(err);
        },
      });
    });
  </script>
  <!-- 注意,在json文件中id值不用加双引号,否则数字就会变成字符串格式显示 -->
</html>

loading加载:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../jQuery/jQuery.min.js"></script>
  </head>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    ul li {
      list-style: none;
      padding: 10px;
      /* 清除浮动 */
      overflow: hidden;
    }
    img {
      width: 200px;
      float: left;
    }
    #loading {
      width: 100%;
      height: 100%;
      display: block;
      position: fixed;
      left: 0;
      top: 0;
      background: rgba(0, 0, 0, 0.5);
    }
    /* 也可以找一张动图当做加载 */
    #loading div img {
      width: 200px;
      height: 200px;
      position: absolute;
      left: 0;
      top: 0;
      margin: auto;
      bottom: 0;
      right: 0;
      font-size: 20px;
    }
  </style>
  <!-- 钩子函数要加的足够靠前 -->
  <script>
    $(window).ajaxSend(function () {
      console.log("显示loading");
      $("#loading").css("display", "block");
    });

    $(window).ajaxComplete(function () {
      console.log("隐藏loading");
      $("#loading").css("display", "none");
    });

    $(window).ready(function () {
      console.log("jquery的钩子");
      $("#list").empty();
    });
  </script>
  <body>
    <button id="btn1">第一页</button>
    <button id="btn2">第二页</button>
    <button id="btn3">第三页</button>
    <ul id="list"></ul>
    <div id="loading">
      <div><img src="../img/1.gif" alt="" /></div>
    </div>
  </body>
  <script>
    // $(window).ajaxStart(function () {
    //   console.log("显示loading");
    // });
    // // 默认显示第一页
    // getList(1);
    // $(window).ajaxStop(function () {
    //   console.log("隐藏loading");
    // });
    // loading可以让用户耐心等待,不再点击按钮,降低服务器的压力

    // 默认显示第一页
    getList(1);
    function getList(num) {
      $.ajax({
        // 显示loading
        url: `https://m.maizuo.com/gateway?cityId=410100&pageNum=${num}&pageSize=3&type=2&k=9539570`,
        //   即便不要求同源,后端也会有自己的请求头验证:
        headers: {
          "X-Client-Info":
            ' {"a":"3000","ch":"1002","v":"5.2.1","e":"16760061975909780510015489","bc":"410100"}',

          "X-Host": " mall.film-ticket.film.list",
        },
        success: function (res) {
          console.log(res.data.films);
          render(res.data.films);
          //隐藏loading
        },
      });
    }
    function render(list) {
      var newList = list.map(
        (item) => `
      <li>
        <img src="${item.poster}"/>
        <h3>${item.name}</h3>
        <div>国家:${item.nation}</div>
        </li>`
      );
      $("#list").html(newList.join(" "));
    }

    $("#btn1").click(function () {
      getList(1);
    });
    $("#btn2").click(function () {
      getList(2);
    });
    $("#btn3").click(function () {
      getList(3);
    });
  </script>
</html>

钩子函数:
钩子函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值