MUI AJAX Raw請求數據


提交接口數據,接口方提供的是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 };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM