LapisSea

Untitled

Mar 2nd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Ajax={
  2.     Obj:function Obj(url){
  3.         this.url=url;
  4.         this.xml=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
  5.         this.headers=[];
  6.         this.headers=function(headers){
  7.             this.headers=headers;
  8.             return this;
  9.         };
  10.         this.load=function(onLoad){
  11.             var xml=this.xml;
  12.             xml.addEventListener("loadend", function(){onLoad(xml.responseText);});
  13.             return this;
  14.         };
  15.         this.error=function(onError){
  16.             this.xml.addEventListener("error", onError);
  17.             return this;
  18.         };
  19.         this.abort=function(onAbort){
  20.             this.xml.addEventListener("abort", onAbort);
  21.             return this;
  22.         };
  23.         this.progress=function(onProgress){
  24.             this.upload.onprogress=onProgress;
  25.             return this;
  26.         };
  27.     },
  28.     get:function get(url){
  29.         var obj=new Ajax.Obj(url);
  30.         obj.run=function(){
  31.             obj.xml.open("GET",obj.url,true);
  32.             for(name in obj.headers)obj.xml.setRequestHeader(name, obj.headers[name]);
  33.             obj.xml.send();
  34.         };
  35.         return obj;
  36.     },
  37.     post:function post(url,data){
  38.         var obj=new Ajax.Obj(url);
  39.         obj.run=function(){
  40.             obj.xml.open("POST",obj.url,true);
  41.             if(typeof data==="object"){
  42.                 obj.xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  43.                 var path=[],str="";
  44.                 var f=function(obj){
  45.                     switch(typeof obj){
  46.                     case "undefined":return;
  47.                     case "object":{
  48.                         for(name in obj){
  49.                             path.push(name);
  50.                             f(obj[name]);
  51.                             path.pop();
  52.                         }
  53.                         return;
  54.                     }
  55.                     }
  56.                     if(path.length==0)return;
  57.                     str+=encodeURIComponent(path[0]);
  58.                     for(var i=1;i<path.length;i++){
  59.                         str+=encodeURIComponent("["+path[i]+"]");
  60.                     }
  61.                     str+="="+encodeURIComponent(obj)+"&";
  62.                    
  63.                 }
  64.                 f(data);
  65.                 data=str.substr(0,str.length-1);
  66.             }
  67.             for(name in obj.headers)obj.xml.setRequestHeader(name, obj.headers[name]);
  68.             obj.xml.send(data);
  69.         };
  70.         return obj;
  71.     }
  72. }
Add Comment
Please, Sign In to add comment