function ajax(ops){
ops.type = ops.type || “get”;
ops.data = ops.data || “”;
ops.url = ops.type==“get” ? ops.url + “?” + ops.data : ops.url;
var xhr = new XMLHttpRequest();
xhr.open(ops.type, ops.url);
if(ops.type == “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);
}
}
}
ajax网络请求的封装
最新推荐文章于 2022-09-24 11:50:32 发布