Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // query = querystring.parse( location.search.substr(1) );
- // cookie = querystring.parse( document.cookie, /;\s*/ );
- querystring = {
- parse: function(s, sep, eq) {
- var o = {};
- eq = eq || '=';
- if (s) {
- var a = s.split(sep || '&'), i = 0, l = a.length, b, k, v;
- while (i < l) {
- b = a[i++].split(eq);
- k = b[0];
- v = b[1];
- if (k !== '') {
- k = decodeURIComponent(k);
- v = v == undefined ? '' : decodeURIComponent(v);
- if (k in o) {
- if (typeof o[k] == 'string') {
- o[k] = new Array(o[k]);
- }
- o[k].push(v);
- }
- else {
- o[k] = v;
- }
- }
- }
- }
- return o;
- },
- stringify: function(o, sep, eq) {
- var p, i, l, a, b = [];
- sep = sep || '&';
- eq = eq || '=';
- for (p in o) {
- if ( o.hasOwnProperty(p) ) {
- a = Array.prototype.concat(o[p]);
- p = encodeURIComponent(p);
- for (i = 0, l = a.length; i < l; ++i) {
- b.push( p + ( a[i] === '' ? '' : eq + encodeURIComponent(a[i]) ) );
- }
- }
- }
- return b.join(sep);
- }
- };
- /**
- * Ajax-обертка
- */
- function Ajax() {
- var t = this;
- t.parseResponse = function(xhr, type) {
- if (type) {
- switch ( type.toLowerCase() ) {
- case 'document':
- case 'xml':
- return xhr.responseXML;
- case 'json':
- return JSON.parse(xhr.responseText);
- case 'script':
- return eval(xhr.responseText);
- }
- }
- return xhr.responseText;
- }
- t.doRequest = function(method, url, data, options) {
- method = method.toUpperCase();
- options = options || {};
- var xhr = new XMLHttpRequest, headers = options.headers;
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if (xhr.status >= 200 && xhr.status < 300) {
- if (options.done) {
- options.done(t.parseResponse(xhr, options.dataType), xhr);
- }
- }
- else if (options.fail) {
- options.fail(xhr.responseText, xhr);
- }
- }
- }
- xhr.open(method, url);
- if (headers) {
- for (var k in headers) {
- if ( headers.hasOwnProperty(k) ) {
- xhr.setRequestHeader(k, headers[k]);
- }
- }
- }
- xhr.send(data);
- };
- /**
- * Отправить данные методом POST
- *
- * @param url адрес документа
- * @param data передаваемые параметры
- * @param options.headers дополнительные заголовки
- * @param options.dataType тип получаемых данных: 'xml' == 'document' | 'json' | 'script'
- * @param options.done function(data, xhr) { ... } вызывается при успешном выполнении запроса
- * @param options.fail function(err, xhr) { ... } вызывается при возвращении сервером статуса отличного от 2XX
- */
- t.post = function(url, data, options) {
- if (data && data.constructor == Object) {
- if (options == undefined) {
- options = {};
- }
- if (options.headers == undefined) {
- options.headers = {};
- }
- var headers = options.headers, type;
- for (var k in headers) {
- if ( headers.hasOwnProperty(k) && k.toLowerCase() == 'content-type') {
- type = headers[k];
- break;
- }
- }
- if ( ( type == undefined && (headers['Content-Type'] = 'application/x-www-form-urlencoded') ) || type.split(';')[0] == 'application/x-www-form-urlencoded' ) {
- data = querystring.stringify(data);
- }
- }
- t.doRequest('post', url, data, options);
- };
- t.put = function(url, data, options) {
- t.doRequest('put', url, data, options);
- };
- t.get = function(url, options) {
- t.doRequest('get', url, null, options);
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment