Ajax封装(亲测可用)

function ajax(options) {
let defaults = {
type: "get"
}
Object.assign(defaults, options)
let xhr = null
if (window.VBArray) {
xhr = new ActiveXObject("Msxml2.XMLHTTP")
} else {
xhr = new XMLHttpRequest()
}
xhr.open(defaults.type, defaults.url)
xhr.onload = function () {
defaults.success ? defaults.success(JSON.parse(xhr.response)) : ""
}
if (defaults.type == "get") {
xhr.send()
}
if (defaults.type == "post") {
xhr.setRequestHeader("content-Type", "application/x-www-from-urlencoded")
xhr.send(defaults.data)
}
}

猜你喜欢

转载自www.cnblogs.com/sunyang-001/p/10827908.html
今日推荐