ajax原理和函数封装

1.ajax的原理?
// ajax(对ajax进行封装)
function ajax({ type = "GET", url, data, success }) {
    let xhr = new XMLHttpRequest();
    xhr.open(type, url);
    xhr.send(data);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            let msg = JSON.parse(xhr.responseText);
            success(msg);
        }
    }
}
使用时直接调用ajax()
ajax({
type:"GET",
url:"",
data:{},
success(){
}
})
发布了12 篇原创文章 · 获赞 9 · 访问量 191

猜你喜欢

转载自blog.csdn.net/weixin_43279985/article/details/103900266