Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ajax={
- Obj:function Obj(url){
- this.url=url;
- this.xml=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
- this.headers=[];
- this.headers=function(headers){
- this.headers=headers;
- return this;
- };
- this.load=function(onLoad){
- var xml=this.xml;
- xml.addEventListener("loadend", function(){onLoad(xml.responseText);});
- return this;
- };
- this.error=function(onError){
- this.xml.addEventListener("error", onError);
- return this;
- };
- this.abort=function(onAbort){
- this.xml.addEventListener("abort", onAbort);
- return this;
- };
- this.progress=function(onProgress){
- this.upload.onprogress=onProgress;
- return this;
- };
- },
- get:function get(url){
- var obj=new Ajax.Obj(url);
- obj.run=function(){
- obj.xml.open("GET",obj.url,true);
- for(name in obj.headers)obj.xml.setRequestHeader(name, obj.headers[name]);
- obj.xml.send();
- };
- return obj;
- },
- post:function post(url,data){
- var obj=new Ajax.Obj(url);
- obj.run=function(){
- obj.xml.open("POST",obj.url,true);
- if(typeof data==="object"){
- obj.xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- var path=[],str="";
- var f=function(obj){
- switch(typeof obj){
- case "undefined":return;
- case "object":{
- for(name in obj){
- path.push(name);
- f(obj[name]);
- path.pop();
- }
- return;
- }
- }
- if(path.length==0)return;
- str+=encodeURIComponent(path[0]);
- for(var i=1;i<path.length;i++){
- str+=encodeURIComponent("["+path[i]+"]");
- }
- str+="="+encodeURIComponent(obj)+"&";
- }
- f(data);
- data=str.substr(0,str.length-1);
- }
- for(name in obj.headers)obj.xml.setRequestHeader(name, obj.headers[name]);
- obj.xml.send(data);
- };
- return obj;
- }
- }
Add Comment
Please, Sign In to add comment