Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- querystring = {
- parse: function(str, sep, eq) {
- eq = eq || '=';
- var o = {};
- if (str) {
- var sp = str.split(sep || '&'),
- i = 0,
- l = sp.length;
- while (i < l) {
- var sp1 = sp[i++].split(eq);
- if (sp1[0] !== '') {
- o[sp1[0]] = sp1[1] === undefined ? '' : decodeURIComponent(sp1[1]);
- }
- }
- }
- return o;
- },
- stringify: function(obj, sep, eq) {
- sep = sep || '&';
- eq = eq || '=';
- var a = [];
- for (var i in obj) {
- a.push( i + (obj[i] !== '' ? eq + encodeURIComponent(obj[i]) : '') );
- }
- return a.join(sep);
- }
- };
- /**
- * Ajax-обертка
- */
- ajax = new (function(){
- var request = this.request = function(method, url, data, options) {
- options = options || {};
- var xhr = new XMLHttpRequest;
- if (options.done || options.fail) {
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if (xhr.status >= 200 && xhr.status < 300) {
- if (options.done) {
- options.done(xhr.response);
- }
- return;
- }
- if (options.fail) {
- options.fail(xhr.status, xhr.statusText);
- }
- }
- }
- }
- xhr.open(method, url);
- xhr.responseType = options.type || 'text';
- if (options.before) {
- options.before(xhr);
- }
- // xhr.send(data);
- try {
- xhr.send(data);
- }
- catch (e) {}
- };
- this.get = function(url, options) {
- request('GET', url, null, options);
- };
- this.rawpost = function(url, data, options) {
- request('POST', url, data, options);
- };
- this.put = function(url, data, options) {
- request('PUT', url, data, options);
- };
- /**
- * Отправить данные методом POST
- *
- * @param url адрес документа
- * @param data ассоциативный массив
- * @param options.type 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' тип передаваемых данных в options.done
- * @param options.done function(response) { ... } функция-обработчик, которая вызывается при успешном выполнении запроса
- * @param options.fail function(code, text) { ... } функция-обработчик, которая вызывается при возвращении сервером статуса отличного от 2XX
- * @param options.before function(xhr) { ... } вызывается до отправки запроса
- */
- this.post = function(url, data, options) {
- var fn = (options = options || {}).before;
- options.before = function(xhr) {
- if (fn) {
- fn(xhr);
- }
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- }
- this.rawpost( url, querystring.stringify(data), options );
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment