首页 > 代码库 > Ajax封装
Ajax封装
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>jsonp</title></head><body><script type="text/javascript" src="jquery-1.6.2.js"></script><!-- <script type="text/javascript" src="http://www.mamicode.com/jquery-1.11.1.js"></script> --><script type="text/javascript">/*Ajax =function(){ function request(url,opt){ function fn(){} var async = opt.async !== false, method = opt.method || ‘GET‘, data = http://www.mamicode.com/opt.data || null,>*/</script><script type="text/javascript">(function(root){ if(typeof(Ajax)=="undefined"&&!Ajax) var Ajax={}; Ajax.request=function(obj){ var xhr=new Ajax.creat(); return xhr.request(obj); } Ajax.creat=function(){ this.ajax=this.get(); } Ajax.creat.prototype={ get:function(){ try{return new XMLHttpRequest();}catch(e){} try{return new ActiveXObject(‘Msxml2.XMLHTTP.6.0‘);}catch(e){} try{return new ActiveXObject(‘Msxml2.XMLHTTP.4.0‘);}catch(e){} try{return new ActiveXObject(‘Msxml2.XMLHTTP.3.0‘);}catch(e){} try{return new ActiveXObject(‘Msxml2.XMLHTTP‘);}catch(e){} try{return new ActiveXObject(‘MSXML3.XMLHTTP‘);}catch(e){} try{return new ActiveXObject(‘MSXML.XMLHTTP‘);}catch(e){} try{return new ActiveXObject(‘Microsoft.XMLHTTP‘);}catch(e){} try{return new ActiveXObject(‘MSXML2.ServerXMLHTTP‘);}catch(e){} return null; }, request:function(obj){ var self=this,ajax=self.ajax; if(typeof(obj)!=="object"||ajax==null) return; var url=obj.url, type=obj.type||"GET", data=obj.data||null, async=typeof(obj.async)=="boolean"? obj.async : true, success=obj.success||new Function(), error=obj.error||new Function(); if(typeof(data)=="object") data=self.objectToStr(data); if(type=="GET"&&data) url+= (url.indexOf("?")==-1?"?":"&")+data; ajax.open(type,url,async); if(type=="POST") ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded;"); ajax.send(data); ajax.onreadystatechange=function(){ self.handle(ajax,success,error); } return ajax; }, objectToStr:function(s){ var str=""; for(var x in s){ str+= x+"="+s[x]+"&"; } str=str.substring(0,str.lastIndexOf("&")); return str; }, handle:function(xhr,success,error){ if(xhr.readyState==4){ var status=xhr.status; if(status>=200&&status<=300) success(xhr); else error(xhr); }else{} } } root.ajax={request:Ajax.request}})(window)//url,type,data,async,success,error</script></body></html>
Ajax封装
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。