stuppid_bot

ajax

Apr 17th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ajax = (function(){
  2.     var  _ = {};
  3.  
  4.     /* options = {success: function(request) {...}, fail: function(code, text) {...} } */
  5.     _.plain = function(method, url, options, params) {
  6.         options = options || {};
  7.         var req = new XMLHttpRequest;        
  8.  
  9.         if (options.success || options.fail) {
  10.             req.onreadystatechange = function() {
  11.                 if (req.readyState  == 4) {
  12.                     if (req.status >= 200 && req.status < 300) {
  13.                         if (options.success) {
  14.                             options.success(req);    
  15.                         }
  16.                                
  17.                         return;                                              
  18.                   }
  19.                                              
  20.                   if (options.fail) {
  21.                       options.fail(req.status, req.statusText);
  22.                   }                  
  23.               }
  24.           }        
  25.       }
  26.  
  27.       req.open(method, url);
  28.  
  29.       if (method.toUpperCase() == 'POST') {
  30.           req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  31.           params = buildQueryString(params);
  32.       }
  33.  
  34.         if (options.before) {
  35.             options.before(req);
  36.         }
  37.  
  38.         req.send(params || null);
  39.     }
  40.  
  41.     _.get = function(url, options) {
  42.         _.plain('GET', url, options, null);
  43.     }
  44.  
  45.     _.post = function(url, params, options) {
  46.         _.plain('POST', url, options, params);
  47.     }
  48.  
  49.     return _;
  50. })();
Advertisement
Add Comment
Please, Sign In to add comment