Advertisement
Guest User

vanilla-ajax-v0.01

a guest
Feb 20th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  | Vanilla JavaScript AJAX Requests
  3.  | = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  4.  | @author:  Tommy Vercety
  5.  | @version: v0.01 alpha
  6.  | @license: MIT
  7.  |
  8.  | Works on almost all browsers. Implemented getJSON and postJSON methods. For
  9.  | everything else use make();
  10.  |
  11.  | Example:
  12.  | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13.  |
  14.  | var ajax = new MY.VanillaAjax();
  15.  |
  16.  | ajax.getJSON("http://httpbin.org/get?hello=world", function(data) {
  17.  |     console.log(data);
  18.  | });
  19.  |
  20.  | var data = JSON.stringify({
  21.  |     name: "tommy",
  22.  |     pass: "qwerty"
  23.  | });
  24.  |
  25.  | ajax.postJSON("http://httpbin.org/post", data, function(data) {
  26.  |     console.log(data);
  27.  | });
  28.  |
  29.  | ISSUES:
  30.  | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  31.  |
  32.  | - some problems sending POST data!
  33.  |
  34.  */
  35.  
  36. var MY = MY || {}; // namespace
  37.  
  38. MY.VanillaAjax = (function() {
  39.  
  40.     /**
  41.      * Constructor.
  42.      *
  43.      * Get the right httpRequest based on your browser
  44.      * @return object httpRequest instance
  45.      */
  46.     function vanillaAjax() {
  47.         vanillaAjax.getHttpRequest = (function () {
  48.             var xmlHttpFactories = [
  49.                 function() { return new XMLHttpRequest(); },
  50.                 function() { return new ActiveXObject("Msxml3.XMLHTTP"); },
  51.                 function() { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); },
  52.                 function() { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); },
  53.                 function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
  54.                 function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
  55.             ];
  56.             return function() {
  57.                 var xmlFactory, i, len;
  58.                 for(i = 0, len = xmlHttpFactories.length; i < len; i++) {
  59.                     xmlFactory = xmlHttpFactories[i];
  60.                     try {
  61.                         return xmlFactory();
  62.                     } catch (error) {
  63.                         console.log(error);
  64.                     }
  65.                 }
  66.                 return null;
  67.             };
  68.         })();
  69.     }
  70.  
  71.     /**
  72.      * Make ajax call
  73.      *
  74.      * @param  object options
  75.      * @return mixed  ajax call data
  76.      */
  77.     vanillaAjax.prototype.make = function(options) {
  78.  
  79.         var httpRequest, requestUrl, type, success, error, contentType, accept, data;
  80.  
  81.         httpRequest = vanillaAjax.getHttpRequest();
  82.         options     = options || {};
  83.  
  84.         requestUrl  = options.url;
  85.         type        = options.type || 'GET';
  86.         success     = options.success || function () {};
  87.         error       = options.error || function () {};
  88.         contentType = options.contentType || '';
  89.         accept      = options.accept || '';
  90.         data        = options.data || null;
  91.  
  92.         httpRequest.onreadystatechange = function() {
  93.             if (httpRequest.readyState === 4) {
  94.                 switch(Math.floor(httpRequest.status / 100)) {
  95.                     case 2:
  96.                         success(httpRequest.responseText);
  97.                         break;
  98.                     default:
  99.                         error(httpRequest.responseText);
  100.                         break;
  101.                 }
  102.             }
  103.         };
  104.  
  105.         httpRequest.open(type, requestUrl, true);
  106.         httpRequest.setRequestHeader('Content-Type', contentType);
  107.         httpRequest.setRequestHeader('Accept', accept);
  108.         return httpRequest.send(data);
  109.     };
  110.  
  111.     /**
  112.      * Direct JSON GET Requst
  113.      *
  114.      * @param  string   url
  115.      * @param  fucntion success
  116.      * @param  function error
  117.      * @return mixed    ajax call data
  118.      */
  119.     vanillaAjax.prototype.getJSON = function(url, success, error) {
  120.         var options = {
  121.             url: url,
  122.             type: 'GET',
  123.             contentType: 'application/json; charset=utf-8',
  124.             accept: 'application/json',
  125.             success: function(data) {
  126.                 if(!success) {return;}
  127.                 if(data) {
  128.                     return success(JSON.parse(data));
  129.                 } else {
  130.                     return success();
  131.                 }
  132.             },
  133.             error: function(err) {
  134.                 if(!error) {return;}
  135.                 if(err) {
  136.                     return error(JSON.parse(err));
  137.                 } else {
  138.                     return error();
  139.                 }
  140.             }
  141.         };
  142.         return vanillaAjax.prototype.make(options);
  143.     };
  144.  
  145.     /**
  146.      * Direct JSON POST Requst
  147.      *
  148.      * @param  string   url
  149.      * @param  object   data    data to be sent
  150.      * @param  fucntion success
  151.      * @param  function error
  152.      * @return mixed    ajax call data
  153.      */
  154.     vanillaAjax.prototype.postJSON = function(url, data, success, error) {
  155.         var options = {
  156.             url: url,
  157.             type: 'POST',
  158.             contentType: 'application/json; charset=utf-8',
  159.             accept: 'application/json',
  160.             data: data,
  161.             success: function(data) {
  162.                 if(!success) {return;}
  163.                 if(data) {
  164.                     return success(JSON.parse(data));
  165.                 } else {
  166.                     return success();
  167.                 }
  168.             },
  169.             error: function(err) {
  170.                 if(!error) {return;}
  171.                 if(err) {
  172.                     return error(JSON.parse(err));
  173.                 } else {
  174.                     return error();
  175.                 }
  176.             }
  177.         };
  178.         return vanillaAjax.prototype.make(options);
  179.     };
  180.  
  181.     return vanillaAjax;
  182.  
  183. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement