• 封装jquery的ajax


     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>jquery ajax封装</title>
     6     <script src="jquery.min.js"></script>
     7 </head>
     8 
     9 <body>
    10     <script>
    11     $(function() {
    12         /**
    13          * ajax封装
    14          * url 发送请求的地址
    15          * data 发送到服务器的数据,数组存储,如:{"username": "张三", "password": 123456}
    16          * succCallback 成功回调函数
    17          * errorCallback 失败回调函数
    18          * type 请求方式("POST" 或 "GET"), 默认已经设置为 "POST"
    19          * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
    20          * reference jquery-1.7.1.js
    21          */
    22 
    23         //插入loading
    24         /*var html = "";
    25         html += '<div class="js_loading">';
    26         html += '<div class="mask"></div>';
    27         html += '<div class="loading">';
    28         html += '<span><img src="loading.gif"></span>';
    29         html += '</div>';
    30         html += '</div>';
    31         $("body").append(html);*/
    32 
    33         function $ajax(url, postData, succCallback, errorCallback, type, dataType) {
    34             var type = type || "post";
    35             var dataType = dataType || "json";
    36             $.ajax({
    37                 type: type,
    38                 url: url,
    39                 data: postData,
    40                 dataType: dataType,
    41                 beforeSend: function() { //开始loading
    42                     //$(".js_loading").show();
    43                 },
    44                 success: function(res) {
    45                     /*if (res.success) {
    46                         if (succCallback) {
    47                             succCallback(res);
    48                         }
    49                     } else {
    50                         if (errorCallback) {
    51                             errorCallback(res);
    52                         }
    53                     }*/
    54                 },
    55                 complete: function() { //结束loading
    56                     //$(".js_loading").remove();
    57                     //$(".js_loading").hide();
    58                 }
    59             });
    60         }
    61 
    62         var postData = { moduleId: "fb750e3c-808f-4497-bf4a-f6bf6726a66e" } || {};
    63         //console.log(postData);
    64 
    65         /*$ajax("http://localhost:6688/AuthorizeManage/ModuleColumn/GetColModel", postData, function(res) {
    66             console.log(res);
    67         }, function(res) {
    68 
    69         }, "GET");*/
    70 
    71     });
    72     </script>
    73 </body>
    74 </html>
    业精于勤荒于嬉,行成于思毁于随
  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/qixianchuan/p/11162435.html
Copyright © 2020-2023  润新知