封装jquery的ajax

JavaScript与JSON相关内容转载
该博客为转载内容,原链接为https://www.cnblogs.com/qixianchuan/p/11162435.html,涉及JavaScript和JSON相关信息技术知识。
 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>

 

转载于:https://www.cnblogs.com/qixianchuan/p/11162435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值