function myAjaxPromise(options) {
return new Promise(function (resolve, reject) {
const xhr = new window.XMLHttpRequest()
let parms = formatParma(options.data)
if (options.method.toUpperCase() == 'GET') {
xhr.open(options.method, options.url + '?' + parms)
xhr.send()
} else if (options.method.toUpperCase() == 'POST') {
xhr.open(options.method, options.url)
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
xhr.send(parms)
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
let result = xhr.responseText
result = JSON.parse(result)
resolve(result)
} else {
reject(xhr.status + xhr.statusText)
}
}
}
})
}
function formatParma(obj) {
let arr = []
for (let key in obj) {
let item = `${key}=${obj[key]}`
arr.push(item)
}
let str = arr.join('&')
return str
}