Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // requires common.js
- // Namespace Ajax
- var Ajax = {
- init: function (defaults) {
- function ajax(opts) {
- opts = extend({
- method: 'GET',
- url: '',
- params: {}, // параметры URL
- cache: ajax.defaults.cache,
- async: true,
- user: '',
- password: '',
- timeout: ajax.defaults.timeout,
- responseType: 0, // string
- overrideMimeType: 0, // string
- withCredentials: ajax.defaults.withCredentials,
- headers: {},
- data: null,
- }, opts);
- var methods = {};
- var xhr = new XMLHttpRequest;
- var params = clone(opts.params);
- if (opts.method === 'GET' && opts.cache === false) {
- params.__t = new Date().getTime(); // url?__t=1410142975437
- }
- var url = opts.url;
- if (!isEmpty(params)) {
- url = url + (url.indexOf('?') == -1 ? '?' : '&') + buildQuery(params);
- }
- xhr.open(opts.method, url, opts.async, opts.user, opts.password);
- if (opts.timeout) {
- xhr.timeout = opts.timeout;
- }
- if (opts.responseType) {
- xhr.responseType = opts.responseType;
- }
- if (opts.overrideMimeType) {
- xhr.overrideMimeType(opts.overrideMimeType);
- }
- xhr.withCredentials = opts.withCredentials;
- var headers = extend(Ajax.normalizeHeaders(ajax.defaults.headers)
- , Ajax.normalizeHeaders(opts.headers));
- for (var h in headers) {
- xhr.setRequestHeader(h, headers[h]);
- }
- xhr.onload = function () {
- if (xhr.status >= 200 && xhr.status < 300) {
- if (methods.done) {
- methods.done(xhr.response, xhr);
- }
- } else {
- if (methods.fail) {
- methods.fail(xhr.response, xhr.status, xhr);
- }
- }
- if (methods.always) {
- methods.always(xhr.response, xhr.status, xhr);
- }
- };
- xhr.send(opts.data);
- var promises = {};
- each(['done', 'fail', 'always'], function (key) {
- promises[key] = function (cb) {
- methods[key] = cb;
- return promises;
- };
- });
- return promises;
- }
- ajax.get = function(url, opts) {
- opts = opts ? clone(opts) : {};
- opts.method = 'GET';
- opts.url = url;
- return ajax(opts);
- };
- ajax.post = function(url, data, opts) {
- opts = opts ? clone(opts) : {};
- opts.method = 'POST';
- opts.url = url;
- if (false === data instanceof FormData) {
- opts.headers = opts.headers || {};
- opts.headers['content-type'] = 'application/x-www-form-urlencoded';
- data = buildQuery(data);
- }
- opts.data = data;
- return ajax(opts);
- };
- ajax.defaults = extend({
- headers: {},
- timeout: 0,
- withCredentials: false,
- cache: true,
- }, defaults);
- return ajax;
- },
- normalizeHeaders: function (headers) {
- var r = {};
- for (var k in headers) {
- if (hasOwn(headers, k)) {
- r[trim(k).toLowerCase()] = headers[k];
- }
- }
- return r;
- },
- };
- var ajax = Ajax.init();
- // f(args).done(function () {}).fail(function () {}).always(function () {});
- // ajax(opts)
- // ajax.get(url[, opts])
- // ajax.post(url, data[, opts])
- // GET /search?q=%D1%81%D0%B8%D1%81%D1%8C%D0%BA%D0%B8&__t=1410145166266
- // ajax.get('/search', {cache: false, params: {q: 'сиськи'}}).done(function (data) { ... });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement