UNPKG

ui-template-muc

Version:

A fantastic mobile ui lib implement by Vue

56 lines (55 loc) 2.16 kB
import axios from 'axios' export default function (url, param, timeout = 8000) { return axios({ method: 'post', url: url, data: param, timeout: timeout, withCredentials: true, headers: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded' }, transformRequest: [function (data) { if (!data) { return };//get请求,无需处理 function each (object, callback) { let i = 0 let length = object.length let isObj = length === undefined || Object.prototype.toString.call(object) === '[object Function]' if (isObj) { for (let name in object) { if (callback.call(object[name], name, object[name]) === false) { break } } } else { for (let value = object[0]; i < length && callback.call(value, i, value) !== false; value = object[++i]) { } } return object } function serialize (params, obj, scope) { let type let array = Object.prototype.toString.call(obj) === '[object Array]' let hash = Object.prototype.toString.call(obj) === '[object Object]' each(obj, function (key, value) { type = Object.prototype.toString.call(value) if (scope) key = scope + '[' + (hash || type === '[object Object]' || type === '[object Array]' ? key : '') + ']' if (!scope && array) params.add(value.name, value.value) else if (type === '[object Array]' || (type === '[object Object]')) { serialize(params, value, key) } else params.add(key, value) }) } let param = function (obj) { let params = [] params.add = function (key, value) { if (Object.prototype.toString.call(value) === '[object Function]') value = value() if (value == null) value = '' this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)) } serialize(params, obj) return params.join('&').replace(/%20/g, '+') } return param(data) }] }) }