欢迎来到博客园~~~

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)
}
}
posted @ 2019-05-07 20:38  孙~洋  阅读(254)  评论(0编辑  收藏  举报