jQuery中的promise实例

jQuery中的promise实例

一、总结

一句话总结:

直接在ajax后调用then方法,jquery的ajax会返回promise对象
  <script>
    $(function () {
      $('#btn').on('click', function () {
        $.ajax({
          url: './data.json',
          type: 'get',
          dataType: 'json'
        })
          .then(function (data) {
            console.log(data)
          })
      })
    });
  </script>

二、jQuery中的promise实例

博客对应课程的视频位置:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5   <meta charset="UTF-8">
 6   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7   <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8   <title>Document</title>
 9 </head>
10 
11 <body>
12 
13   <input type="button" value="获取数据" id="btn">
14 
15   <script src="./node_modules/jquery/dist/jquery.min.js"></script>
16 
17   <script>
18     $(function () {
19       $('#btn').on('click', function () {
20         $.ajax({
21           url: './data.json',
22           type: 'get',
23           dataType: 'json'
24         })
25           .then(function (data) {
26             console.log(data)
27           })
28       })
29     });
30   </script>
31 </body>
32 
33 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/12776377.html