AJAX的基础封装

function ajax(ops){
    ops.method= ops.method || "get";
    ops.data = ops.data || "";
    ops.url = ops.method=="get" ? ops.url + "?" + ops.data : ops.url;
    var xhr = new XMLHttpRequest();
    xhr.open(ops.method, ops.url);
    if(ops.method== "get"){
        xhr.send();
    }else{
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xhr.send(ops.data);
    }
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            ops.success(xhr.responseText);
        }
    }
}
发布了63 篇原创文章 · 获赞 234 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_42881768/article/details/104801415