请求方式,请求地址,请求参数,请求参数类型,请求返回结果
<script>
function ajax(options) {
// 创建XMLHttpRequest对象
const xhr = new XMLHttpRequest();
// 初始化参数的内容
options = options || {}
options.type = (options.type || 'GET').toUpperCase()
options.dataType = options.dataType || 'json'
const params = options.data
// 发送请求
if (options.type === "GET") {
//第三个参数 async:布尔值,表示是否异步执行操作
xhr.open('GET', options.url + "?" + getParams(params), true)
xhr.send(null)
} else if (options.type === "POST") {
xhr.open("POST", options.url, true)
xhr.send(params)
}
// 接收请求
xhr.onreadystatechange = func