Advertisement
Guest User

Facade

a guest
Jul 28th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="../libs/jquery-1.11.1.min.js" />
  2.  
  3. var httpModule = (function () {
  4.  
  5.     function makeHttpRequest(resourceUrl, header, data) {
  6.         var getHttpRequest,
  7.             httpRequest,
  8.             deferred = $.Deferred(),
  9.             statusType,
  10.             response;
  11.  
  12.         getHttpRequest = (function () {
  13.             var xmlHttpFactories;
  14.  
  15.             xmlHttpFactories = [
  16.               function () {
  17.                   return new XMLHttpRequest();
  18.               }, function () {
  19.                   return new ActiveXObject("Msxml3.XMLHTTP");
  20.               }, function () {
  21.                   return new ActiveXObject("Msxml2.XMLHTTP.6.0");
  22.               }, function () {
  23.                   return new ActiveXObject("Msxml2.XMLHTTP.3.0");
  24.               }, function () {
  25.                   return new ActiveXObject("Msxml2.XMLHTTP");
  26.               }, function () {
  27.                   return new ActiveXObject("Microsoft.XMLHTTP");
  28.               }
  29.             ];
  30.  
  31.             return function () {
  32.                 var xmlFactory, _i, _len;
  33.                 for (_i = 0, _len = xmlHttpFactories.length; _i < _len; _i++) {
  34.                     xmlFactory = xmlHttpFactories[_i];
  35.                     try {
  36.                         return xmlFactory();
  37.                     } catch (_error) {
  38.  
  39.                     }
  40.                 }
  41.                 return null;
  42.             };
  43.         })();
  44.  
  45.         httpRequest = getHttpRequest();
  46.  
  47.         httpRequest.onreadystatechange = function () {
  48.             if (httpRequest.readyState === 4) {
  49.                 statusType = parseInt(httpRequest.status / 100);
  50.                 if (statusType === 2) {
  51.                     response = JSON.parse(httpRequest.response);
  52.                     deferred.resolve(response);
  53.                 } else {
  54.                     deferred.reject('Something went wrong');
  55.                 }
  56.             }
  57.         };
  58.  
  59.         httpRequest.open(header, resourceUrl, true);
  60.         httpRequest.setRequestHeader('Content-Type', 'application/json');
  61.         httpRequest.setRequestHeader('Accept', 'application/json');
  62.         httpRequest.send(data);
  63.  
  64.         return deferred.promise();
  65.     }
  66.  
  67.     function getJSON(url) {
  68.         return makeHttpRequest(url, 'GET', null);
  69.     }
  70.  
  71.     function postJSON(url, data) {
  72.         data = JSON.stringify(data);
  73.         return makeHttpRequest(url, 'POST', data)
  74.     }
  75.  
  76.     return {
  77.         getJSON: getJSON,
  78.         postJSON: postJSON,
  79.     };
  80. })();
  81.  
  82. var url = 'http://localhost:3000/students';
  83.  
  84. httpModule.postJSON(url,
  85.     {
  86.         name: 'LOL',
  87.         grade: 5
  88.     })
  89.     .then(function (data) {
  90.         if (data) {
  91.             console.log(data);
  92.         }
  93.     }, function (error) {
  94.         if (error) {
  95.             console.log(error);
  96.         }
  97.     })
  98.     .done();
  99.  
  100.  
  101.  
  102. httpModule.getJSON(url)
  103.     .then(function (data) {
  104.         if (data) {
  105.             console.log(data);
  106.         }
  107.     }, function (error) {
  108.         if (error) {
  109.             console.log(error);
  110.         }
  111.     })
  112.     .done();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement