Guest User

JavaScript httpclient

a guest
Jun 27th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5.     <script type="text/javascript">
  6.         //-- Usage examples ----------------------------------------------
  7.         /*
  8.         //Full
  9.         client = new httpClient();
  10.         client.method("get");
  11.         client.baseUrl("Service.aspx");
  12.         client.requestData({name: "John", age: 56, location: "Kansas City"});
  13.         var response = client.makeRequest();
  14.         alert(response);
  15.  
  16.         //Multiple requests
  17.         client = new httpClient();
  18.         client.baseUrl("Service.aspx");
  19.  
  20.         client.requestData("?q=foo");
  21.         var data = client.makeRequest();
  22.         alert(data);
  23.  
  24.         client.requestData("?q=foobar");
  25.         var data = client.makeRequest();
  26.         alert(data);
  27.  
  28.         //Minimal
  29.         client = new httpClient();
  30.         client.baseUrl("Service.aspx?q=test");
  31.         client.makeRequest();
  32.  
  33.         //Minimal, with deafult base URL http://localhost/
  34.         client = new httpClient();
  35.         client.requestData("?q=foobar");
  36.         client.makeRequest();
  37.  
  38.         //Full, with response output contained in an object
  39.         client = new httpClient();
  40.         client.method("get");
  41.         client.baseUrl("Service.aspx");
  42.         client.requestData("?q=test");
  43.         var requestObject = client.makeRequestObject();
  44.         alert(requestObject.MimeType);
  45.         alert(requestObject.charset);
  46.  
  47.         //Custom callback function to handle asychronous httprequests
  48.         myCallback = function (response) {
  49.             document.getElementById("div").innerHTML += response;
  50.         }
  51.  
  52.         client = new httpClient();
  53.  
  54.         client.asynchronous(true);
  55.         client.method("get");
  56.         client.baseUrl("Service.aspx");
  57.         client.callback(myCallback);
  58.  
  59.         client.requestData({ name: "Peter", age: 45, location: "Kansas City" });
  60.         client.makeRequest();
  61.  
  62.         */
  63.  
  64.         function httpClient() {
  65.             this.$baseUrl = "http://localhost/";
  66.             this.$method = "get";
  67.             this.$requestData = "";
  68.             this.$asynchronous = false;
  69.             this.$callbackFunction = "";
  70.             this.$invokeCallback = function (func, response) {
  71.                 func(response);
  72.             }
  73.         }
  74.  
  75.         httpClient.prototype.method = function (requestMethod) {
  76.             this.$method = requestMethod;
  77.         }
  78.  
  79.         httpClient.prototype.baseUrl = function (requestBaseUrl) {
  80.             this.$baseUrl = requestBaseUrl;
  81.         }
  82.  
  83.         $_xmlhttpConstruct = function () {
  84.             var xmlhttp;
  85.             if (window.XMLHttpRequest) {//IE7+, Firefox, Chrome, Opera, Safari
  86.                 return new XMLHttpRequest();
  87.             }
  88.             else {//IE6, IE5
  89.                 try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
  90.                 catch (e) { }
  91.                 try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
  92.                 catch (e) { }
  93.                 try { return new ActiveXObject("Microsoft.XMLHTTP"); }
  94.                 catch (e) { }
  95.             }
  96.         }
  97.  
  98.         httpClient.prototype.setRequestHeader = function (header, value) {
  99.  
  100.         }
  101.  
  102.         httpClient.prototype.callback = function (func) {
  103.             this.$callbackFunction = func; //Does not get set to new callback function on multiple requests
  104.            
  105.         }
  106.  
  107.         httpClient.prototype.asynchronous = function (boolean) {
  108.             this.$asynchronous = boolean;
  109.         }
  110.  
  111.         httpClient.prototype.makeRequest = function () {
  112.  
  113.             //Initializing the xmlhttp object
  114.             var xmlhttp = $_xmlhttpConstruct();
  115.  
  116.             if (this.$requestData == undefined) {
  117.                 xmlhttp.open(this.$method, this.$baseUrl, this.$asynchronous);
  118.             }
  119.             else {
  120.                 if (this.$method == "post") {
  121.  
  122.                     xmlhttp.open(this.$method, this.$baseUrl, this.$asynchronous);
  123.                     xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  124.                     xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  125.                     xmlhttp.setRequestHeader("Cache-Control", "no-cache, must-revalidate"); //HTTP 1.1
  126.                     //xmlhttp.setRequestHeader("Pragma", "no-cache"); //HTTP 1.0
  127.                     //xmlhttp.setRequestHeader("Expires", "Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  128.  
  129.                     xmlhttp.send(this.$requestData);
  130.  
  131.                     invokeCallback = this.$invokeCallback;
  132.                     callbackFunction = this.$callbackFunction;
  133.  
  134.                     if (this.$asynchronous) {
  135.                         xmlhttp.onreadystatechange = function () {
  136.                             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  137.                                 //Callback
  138.                                 invokeCallback(callbackFunction, xmlhttp.responseText);
  139.                             }
  140.                         }
  141.                     }
  142.                     else {
  143.                         return xmlhttp.responseText;
  144.                     }
  145.  
  146.                     //alert("URL: " + this.$baseUrl + "\n" + "Method: " + this.$method + "\n" + "Request data: " + this.$requestData);
  147.                 }
  148.  
  149.                 if (this.$method == "get") {
  150.  
  151.                     xmlhttp.open(this.$method, this.$baseUrl + this.$requestData, this.$asynchronous);
  152.                     xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  153.                     xmlhttp.setRequestHeader("Cache-Control", "no-cache, must-revalidate"); //HTTP 1.1
  154.                     //xmlhttp.setRequestHeader("Pragma", "no-cache"); //HTTP 1.0
  155.                     //xmlhttp.setRequestHeader("Expires", Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  156.                     xmlhttp.send(null);
  157.  
  158.                     invokeCallback = this.$invokeCallback;
  159.                     callbackFunction = this.$callbackFunction;
  160.  
  161.                     if (this.$asynchronous) {
  162.                         xmlhttp.onreadystatechange = function () {
  163.                             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  164.                                 //Callback
  165.                                 invokeCallback(callbackFunction, xmlhttp.responseText);
  166.                             }
  167.                         }
  168.                     }
  169.                     else {
  170.                         return xmlhttp.responseText;
  171.                     }
  172.  
  173.                     //alert("URL: " + this.$baseUrl + "\n" + "Full request URL: " + this.$baseUrl + this.$requestData + "\n" + "Method: " + this.$method + "\n" + "Request data: " + this.$requestData);
  174.                 }
  175.             }
  176.         }
  177.  
  178.         httpClient.prototype.makeRequestObject = function () {
  179.  
  180.             var requestObject = {
  181.                 method: this.$method,
  182.                 url: this.$baseUrl,
  183.                 requestData: this.$requestData,
  184.                 responseData: "",
  185.                 MimeType: "",
  186.                 charset: ""
  187.             }
  188.  
  189.             var xmlhttp;
  190.             if (window.XMLHttpRequest) {//IE7+, Firefox, Chrome, Opera, Safari
  191.                 xmlhttp = new XMLHttpRequest();
  192.             }
  193.             else {//IE6, IE5, IE5.5
  194.                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  195.             }
  196.  
  197.             if (this.$requestData == undefined) {
  198.                 xmlhttp.open(this.$method, this.$baseUrl, false);
  199.             }
  200.             else {
  201.                 //alert(this.$baseUrl + this.$requestData);
  202.                 xmlhttp.open(this.$method, this.$baseUrl + this.$requestData, false);
  203.             }
  204.  
  205.             if (this.$method == "post") {
  206.                 xmlhttp.open(this.$method, this.$baseUrl, false);
  207.                 xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  208.                 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  209.                 xmlhttp.setRequestHeader("Cache-Control", "No-Cache");
  210.                 xmlhttp.send(this.$requestData);
  211.  
  212.                 //alert("URL: " + this.$baseUrl + "\n" + "Method: " + this.$method + "\n" + "Request data: " + this.$requestData);
  213.             }
  214.             if (this.$method == "get") {
  215.                 xmlhttp.open(this.$method, this.$baseUrl + this.$requestData, false);
  216.                 xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  217.                 xmlhttp.setRequestHeader("Cache-Control", "No-Cache");
  218.                 xmlhttp.send();
  219.  
  220.                 //alert("URL: " + this.$baseUrl + "\n" + "Full request URL: " + this.$baseUrl + this.$requestData + "\n" + "Method: " + this.$method + "\n" + "Request data: " + this.$requestData);
  221.             }
  222.  
  223.             var mc = xmlhttp.getResponseHeader("Content-Type");
  224.             mc = mc.split(";");
  225.  
  226.             requestObject.MimeType = mc[0];
  227.             requestObject.charset = mc[1].substring(9);
  228.  
  229.             requestObject.responseData = xmlhttp.responseText;
  230.  
  231.             return requestObject;
  232.         }
  233.  
  234.         httpClient.prototype.requestData = function (data) {
  235.  
  236.             this.$requestData = "";
  237.  
  238.             if (typeof (data) == "object") {
  239.                 var i = 0;
  240.                 for (key in data) {
  241.                     if (i == 0) {
  242.                         if (this.$method == "get") {
  243.                             this.$requestData += "?" + key + "=" + data[key];
  244.                         }
  245.                         if (this.$method == "post") {
  246.                             this.$requestData += key + "=" + data[key];
  247.                         }
  248.                         i++;
  249.                     }
  250.                     else {
  251.                         this.$requestData += "&" + key + "=" + data[key];
  252.                     }
  253.                 }
  254.             }
  255.             else {
  256.                 this.$requestData = data;
  257.             }
  258.         }
  259.  
  260.         //---------------------------------------------------------------------------------
  261.  
  262.     </script>
  263. </head>
  264. <body>
  265.     <div id="debug"></div>
  266.     <script type="text/javascript">
  267.         //Custom callback functions to handle asychronous httprequests
  268.         function testone(response) {
  269.  
  270.             document.getElementById("debug").innerHTML += "<div style='color: green;'>" + response + "</div>";
  271.         }
  272.         function testtwo(response) {
  273.  
  274.             document.getElementById("debug").innerHTML += "<div style='color: red;'>" + response + "</div>";
  275.         }
  276.        
  277.         clientOne = new httpClient();
  278.  
  279.         clientOne.asynchronous(true);
  280.         clientOne.method("get");
  281.         clientOne.baseUrl("Service.aspx");
  282.  
  283.         clientOne.callback(testone);
  284.  
  285.         clientOne.requestData({ name: "Peter", age: 45, location: "Kansas City" });
  286.         clientOne.makeRequest();
  287.  
  288.         //---------------------------------------------------
  289.  
  290.         clientTwo = new httpClient();
  291.  
  292.         clientTwo.asynchronous(true);
  293.         clientTwo.method("get");
  294.         clientTwo.baseUrl("Service.aspx");
  295.  
  296.         clientTwo.callback(testtwo);
  297.  
  298.         clientTwo.requestData({ name: "Mary", age: 45, location: "Kansas City" });
  299.         clientTwo.makeRequest();
  300.        
  301.  
  302.         //Synchronous works!
  303.         /*
  304.         client = new httpClient();
  305.  
  306.         client.asynchronous(false);
  307.         client.method("get");
  308.         client.baseUrl("Service.aspx");
  309.        
  310.         client.requestData({ name: "Peter", age: 45, location: "Kansas City" });
  311.         testone(client.makeRequest());
  312.  
  313.         client.requestData({ name: "Mary", age: 45, location: "Kansas City" });
  314.         testtwo(client.makeRequest());
  315.         */
  316.  
  317.     </script>
  318. </body>
  319. </html>
Advertisement
Add Comment
Please, Sign In to add comment