Ajax插件html
<script src="ajax.js"></script>
<script>
new AjaxClass({type:"get",url:"ajax.text",
isAsyn:true,data:"",callBack:fun}).ajax();
function fun(resText){
console.log(resText)
}
</script>
ajax.js
class AjaxClass{
constructor(obj){
this.obj=obj;
}
ajax(){
let xhr;
if (window.ActiveXObject) {
//ie
xhr = new ActiveXObject("Microsoft.XMLHttp");
} else {
//非ie
xhr = new XMLHttpRequest();
}
this.obj.type=this.obj.toLowerCase();
if(this.obj.type=="get"){
let urlparm=this.obj.url;
if(this.obj.data!=""){
urlparam+="?"+this.obj.data;
}
xhr,open(this.obj.type,urlparam,this.obj.isAsyn)
xhr.send();
}else if(this.obj.type=="post"){
xhr.open(this.obj.type,this.obj.url,this.obj.isAsyn);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//post请求要有请求头
xhr.send(this.obj.data);
//而且参数要写在send里;
}else{
return "传输参数有误";
}
let that =this;
xhr.onreadystatechange=function(){
if(xhr.readystate==4&&xhr.status==200){
that.obj.callBack(xhr.responseText) ;
}
}
jsonp(){
}
} //类在这块结束