菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
362
0

MUI AJAX Raw请求数据

原创
05/13 14:22
阅读数 6103

提交接口数据,接口方提供的是post请求,body - raw ; 

我尝试过JQuery ajax raw 的方式,但是始终无法成功

然后我回想到我用的是mui我就开始考虑用mui.ajax结果就成功了,特此记录下。其实也是也是自己走了弯路,本来用mui,最好的还是用它的全部东东

代码分享一下:

 

 1 (function () {
 2     
 3     window.ax = {
 4         api:"http://127.0.0.1/",  // 这里需要替换为你的接口IP
 5         
 6         // GET   提交方式
 7         // u    --请求地址  
 8         // f    --回调函数   成功 function(data){} ; 失败 function(xhr,type,errorThrown){}
 9         g:function(u,f){
10             mui.get( ax.api + u,{},f,"json");
11         },
12         
13         // POST   提交方式
14         // u    --请求地址  
15         // p    --发送数据   $(id) 例子: "#id",{id:1}
16         // f    --回调函数   成功 function(data){} ; 失败 function(xhr,type,errorThrown){}
17         p:function(u,p,f) {
18             if(typeof(p) === "string") {
19                 p = $(p).serializeObject();
20             }
21             mui.ajax(ax.api + u,{
22                     data: p,
23                     dataType: 'json',
24                     type: 'post',
25                     headers: {'Content-Type':'application/json'},                  
26                     success: f,
27                     error: f
28             });
29         }
30     }
31     
32 })();
33 
34 // 对Date的扩展,将 Date 转化为指定格式的String
35 // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
36 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
37 // 例子: 
38 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
39 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
40 Date.prototype.Format = function (fmt) { //author: zhengsh 2016-9-5 
41     var o = {
42         "M+": this.getMonth() + 1, //月份 
43         "d+": this.getDate(), //
44         "h+": this.getHours(), //小时 
45         "m+": this.getMinutes(), //
46         "s+": this.getSeconds(), //
47         "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
48         "S": this.getMilliseconds() //毫秒 
49     };
50     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
51     for (var k in o)
52     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
53     return fmt;
54 }
55 
56 // 表单对象转换为JSON
57 $.fn.serializeObject = function() {
58     var o = {};
59     var a = this.serializeArray();
60     $.each(a, function() {
61         if(o[this.name]) {
62             if(!o[this.name].push) {
63                 o[this.name] = [o[this.name]];
64             }
65             o[this.name].push(this.value || '');
66         } else {
67             o[this.name] = this.value || '';
68         }
69     });
70     return o;
71 };

 

发表评论

0/200
362 点赞
0 评论
收藏