封装ajax(二)闭包的形式

(function(){
var $=(function(){
function create(){
if(window.XMLHttpRequest){
var xhr=new XMLHttpRequest();
}else{
var xhr=new ActiveXObject("Microsoft.XMLHTTP")
}
return xhr
}
return {
ajax:function(obj){
var xhr=create();
if(obj.async==undefined){
obj.async=true
}
if(obj.method=="get"){
obj.url=obj.url+"?"+obj.param;
obj.param=null;
}
xhr.open(obj.method,obj.url,obj.async)
if(obj.method=="post"){
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
}
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
obj.success(xhr.responseText)
}
}
xhr.send(obj.param)
}
}
})()
window.$=$;
})()
$.ajax({})

原文地址:https://www.cnblogs.com/DebbieBlog/p/5825276.html