const ajax = option => {
const objToString = data => {
data.t = new Date().getTime();
let res = []
for (let key in data) {
res.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
}
return res.join('&')
}
console.log(objToString, '0.对象转换成字符串')
let str = objToString(option.data || {})
var xmlHttp, timer
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest()
} else if (xmlHttp) {
xmlHttp = new ActiveXObject('Microsoft.xmlHttp')
}
if (option.type.toLowerCase() === 'get') {
xmlHttp.open(option.type, option.url + '?t=' + str, true)
xmlHttp.send()
} else {
xmlHttp.open(option.type, option.url, true)
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xmlHttp.send()
}
xmlHttp.onreadystatechange = function () {
clearInterval(timer)
debugger
if (xmlHttp.readyState === 4) {
option.success(xmlHttp.responseText)
} else {
option.error(xmlHttp.responseText)
}
}
if (option.timeout) {
timer = setInterval(function () {
xmlHttp.abort()
clearInterval(timer)
}, option.timeout)
}
}
console