HTTPRequest //JavaScript Class
Class to make remote requests, which can be used on the popular "AJAX".
[UPDATED CODE AND HELP CAN BE FOUND HERE]
1 2 //+ Jonas Raoni Soares Silva 3 //@ http://jsfromhell.com/classes/http-request [v1.0] 4 5 HTTPRequest = function(){}; 6 with({$: HTTPRequest.prototype}){ 7 $.isSupported = function(){ 8 return !!this.getConnection(); 9 }; 10 $.events = ["start", "open", "send", "load", "end"]; 11 $.filter = encodeURIComponent; 12 $.getConnection = function(){ 13 var i, o = [function(){return new ActiveXObject("Msxml2.XMLHTTP");}, 14 function(){return new ActiveXObject("Microsoft.XMLHTTP");}, 15 function(){return new XMLHttpRequest;}]; 16 for(i = o.length; i--;) try{return o[i]();} catch(e){} 17 return null; 18 }; 19 $.formatParams = function(params){ 20 var i, r = []; 21 for(i in params) r[r.length] = i + "=" + (this.filter ? this.filter(params[i]) : params[i]); 22 return r.join("&"); 23 }; 24 $.get = function(url, params, handler, waitResponse){ 25 return this.request("GET", url + (url.indexOf("?") + 1 ? "&" : "?") + this.formatParams(params), null, handler, null, waitResponse); 26 }; 27 $.post = function(url, params, handler, waitResponse){ 28 return this.request("POST", url, params = this.formatParams(params), handler, { 29 "Connection": "close", 30 "Content-Length": params.length, 31 "Method": "POST " + url + " HTTP/1.1", 32 "Content-Type": "application/x-www-form-urlencoded; charset=utf-8" 33 }, waitResponse); 34 }; 35 $.request = function(method, url, params, handler, headers, waitResponse){ 36 var i, o = this.getConnection(), f = handler instanceof Function; 37 try{ 38 o.open(method, url, !waitResponse); 39 waitResponse || (o.onreadystatechange = function(){ 40 var s = $.events[o.readyState]; 41 f ? handler(o) : s in handler && handler[s](o); 42 }); 43 o.setRequestHeader("HTTP_USER_AGENT", "XMLHttpRequest"); 44 for(i in headers) 45 o.setRequestHeader(i, headers[i]); 46 o.send(params); 47 waitResponse && (f ? handler(o) : handler["end"] && handler["end"](o)); 48 return true; 49 } 50 catch(e){ 51 return false; 52 } 53 }; 54 }
Example
1 2 <fieldset> 3 <legend>HTTPRequest example</legend> 4 <input type="button" value="POST request with generic listener and params passage" onclick="genericHandler()" /> 5 <br /><input type="button" value="GET request with specific listener (binded on load and end)" onclick="specificHandler()" /> 6 7 <script type="text/javascript"> 8 //<![CDATA[ 9 10 var r = new HTTPRequest; 11 12 function myHandler(o){ 13 alert("Current event = " + r.events[o.readyState] + 14 "\nAvailable \"responseText.length\" = " + o.responseText.length); 15 } 16 function genericHandler(){ 17 r.post(location.href, {param: "abcde", name: "Jonas", site: "http://jsfromhell.com"}, myHandler); 18 } 19 function specificHandler(){ 20 r.get(location.href, null, {"load": myHandler, "end": myHandler}); 21 } 22 document.write( 23 "<br />Supports XMLHTTPRequest = ".bold() + r.isSupported(), 24 "<br />Encoded with the default filter (\"encodeURIComponent\") = ".bold() + r.formatParams({nameA: "aeiou", nameB: "áéíóú"}) 25 ); 26 27 r.filter = escape; 28 document.write("<br />Encoded with \"escape\" filter = ".bold() + r.formatParams({nameA: "aeiou", nameB: "áéíóú"})); 29 30 r.filter = null; 31 document.write("<br />Encoded with no filtering = ".bold() + r.formatParams({nameA: "aeiou", nameB: "áéíóú"})); 32 33 //]]> 34 </script> 35 </fieldset>