stuppid_bot

Untitled

May 31st, 2013
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // query = querystring.parse( location.search.substr(1) );
  2. // cookie = querystring.parse( document.cookie, /;\s*/ );
  3. querystring = {
  4.     parse: function(s, sep, eq) {
  5.         var o = {};
  6.         eq = eq || '=';      
  7.  
  8.         if (s) {
  9.             var a = s.split(sep || '&'), i = 0, l = a.length, b, k, v;
  10.  
  11.             while (i < l) {
  12.                 b = a[i++].split(eq);
  13.                 k = b[0];
  14.                 v = b[1];                    
  15.  
  16.                 if (k !== '') {
  17.                     k = decodeURIComponent(k);
  18.                     v = v == undefined ? '' : decodeURIComponent(v);
  19.                    
  20.                     if (k in o) {
  21.                         if (typeof o[k] == 'string') {
  22.                             o[k] = new Array(o[k]);
  23.                         }
  24.                        
  25.                         o[k].push(v);
  26.                     }
  27.                     else {
  28.                         o[k] = v;
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.  
  34.         return o;
  35.     },
  36.    
  37.     stringify: function(o, sep, eq) {
  38.         var p, i, l, a, b = [];
  39.         sep = sep || '&';
  40.         eq = eq || '=';      
  41.  
  42.         for (p in o) {
  43.             if ( o.hasOwnProperty(p) ) {
  44.                 a = Array.prototype.concat(o[p]);
  45.                 p = encodeURIComponent(p);
  46.                
  47.                 for (i = 0, l = a.length; i < l; ++i) {
  48.                     b.push( p + ( a[i] === '' ? '' : eq + encodeURIComponent(a[i]) ) );
  49.                 }
  50.             }
  51.         }
  52.  
  53.         return b.join(sep);
  54.     }
  55. };
  56.  
  57. /**
  58.  * Ajax-обертка
  59.  */
  60. function Ajax() {
  61.     var t = this;
  62.    
  63.     t.parseResponse = function(xhr, type) {
  64.         if (type) {
  65.             switch ( type.toLowerCase() ) {
  66.                 case 'document':
  67.                 case 'xml':
  68.                     return xhr.responseXML;
  69.  
  70.                 case 'json':
  71.                     return JSON.parse(xhr.responseText);
  72.  
  73.                 case 'script':
  74.                     return eval(xhr.responseText);
  75.             }
  76.         }
  77.        
  78.         return xhr.responseText;
  79.     }
  80.    
  81.     t.doRequest = function(method, url, data, options) {
  82.         method = method.toUpperCase();
  83.         options = options || {};
  84.         var xhr = new XMLHttpRequest, headers = options.headers;        
  85.        
  86.         xhr.onreadystatechange = function() {
  87.             if (xhr.readyState == 4) {
  88.                 if (xhr.status >= 200 && xhr.status < 300) {
  89.                     if (options.done) {
  90.                         options.done(t.parseResponse(xhr, options.dataType), xhr);
  91.                     }
  92.                 }
  93.                 else if (options.fail) {
  94.                     options.fail(xhr.responseText, xhr);
  95.                 }              
  96.             }
  97.         }
  98.        
  99.         xhr.open(method, url);        
  100.        
  101.         if (headers) {
  102.             for (var k in headers) {
  103.                 if ( headers.hasOwnProperty(k) ) {
  104.                     xhr.setRequestHeader(k, headers[k]);
  105.                 }            
  106.             }
  107.         }
  108.        
  109.         xhr.send(data);  
  110.     };  
  111.    
  112.     /**
  113.      * Отправить данные методом POST
  114.      *  
  115.      * @param url адрес документа
  116.      * @param data передаваемые параметры
  117.      * @param options.headers дополнительные заголовки
  118.      * @param options.dataType тип получаемых данных: 'xml' == 'document' | 'json' | 'script'
  119.      * @param options.done function(data, xhr) { ... } вызывается при успешном выполнении запроса
  120.      * @param options.fail function(err, xhr) { ... } вызывается при возвращении сервером статуса отличного от 2XX
  121.      */
  122.     t.post = function(url, data, options) {
  123.         if (data && data.constructor == Object) {
  124.             if (options == undefined) {
  125.                 options = {};
  126.             }
  127.  
  128.             if (options.headers == undefined) {
  129.                 options.headers = {};
  130.             }
  131.  
  132.             var headers = options.headers, type;
  133.  
  134.             for (var k in headers) {
  135.                 if ( headers.hasOwnProperty(k) && k.toLowerCase() == 'content-type') {
  136.                     type = headers[k];
  137.                     break;
  138.                 }
  139.             }
  140.            
  141.             if ( ( type == undefined && (headers['Content-Type'] = 'application/x-www-form-urlencoded') ) || type.split(';')[0] == 'application/x-www-form-urlencoded' ) {
  142.                 data = querystring.stringify(data);
  143.             }
  144.         }
  145.        
  146.         t.doRequest('post', url, data, options);
  147.     };
  148.    
  149.     t.put = function(url, data, options) {
  150.         t.doRequest('put', url, data, options);
  151.     };
  152.    
  153.     t.get = function(url, options) {
  154.         t.doRequest('get', url, null, options);
  155.     };  
  156. };
Advertisement
Add Comment
Please, Sign In to add comment