提交接口數據,接口方提供的是post請求,body - raw ; ajax
我嘗試過JQuery ajax raw 的方式,可是始終沒法成功
json
而後我回想到我用的是mui我就開始考慮用mui.ajax結果就成功了,特此記錄下。其實也是也是本身走了彎路,原本用mui,最好的仍是用它的所有東東api
代碼分享一下:app
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 };