Skip to content

Instantly share code, notes, and snippets.

@mythz
Created November 2, 2011 19:05
Show Gist options
  • Save mythz/1334560 to your computer and use it in GitHub Desktop.
Save mythz/1334560 to your computer and use it in GitHub Desktop.
Standalone jQuery-like Ajax Client
//Adds $.xhr and jQuery-like $.ajax methods to the prescribed namespace.
//Inspired from David Flanagans excellent cross-platform utils http://www.davidflanagan.com/javascript5/display.php?n=20-1&f=20/01.js
//Includes underscore.js _.each and _.extend methods
//modified to behave like jQuery's $.ajax(), not complete.
(function($) {
var win=window, xhrs = [
function () { return new XMLHttpRequest(); },
function () { return new ActiveXObject("Microsoft.XMLHTTP"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP"); }
],
_xhrf = null;
var hasOwnProperty = Object.prototype.hasOwnProperty,
nativeForEach = Array.prototype.forEach;
var _each = function (o, fn, ctx) {
if (o == null) return;
if (nativeForEach && o.forEach === nativeForEach)
o.forEach(fn, ctx);
else if (o.length === +o.length) {
for (var i = 0, l = o.length; i < l; i++)
if (i in o && fn.call(ctx, o[i], i, o) === breaker) return;
} else {
for (var key in o)
if (hasOwnProperty.call(o, key))
if (fn.call(ctx, o[key], key, o) === breaker) return;
}
};
var _extend = function (o) {
_each(slice.call(arguments, 1), function (a) {
for (var p in a) if (a[p] !== void 0) o[p] = a[p];
});
return o;
};
$.xhr = function () {
if (_xhrf != null) return _xhrf();
for (var i = 0, l = xhrs.length; i < l; i++) {
try {
var f = xhrs[i], req = f();
if (req != null) {
_xhrf = f;
return req;
}
} catch (e) {
continue;
}
}
return function () { };
};
$._xhrResp = function (xhr) {
switch (xhr.getResponseHeader("Content-Type").split(";")[0]) {
case "text/xml":
return xhr.responseXML;
case "text/json":
case "application/json":
case "text/javascript":
case "application/javascript":
case "application/x-javascript":
return win.JSON ? JSON.parse(xhr.responseText) : eval(xhr.responseText);
default:
return xhr.responseText;
}
};
$._formData = function (o) {
var kvps = [], regEx = /%20/g;
for (var k in o) kvps.push(encodeURIComponent(k).replace(regEx, "+") + "=" + encodeURIComponent(o[k].toString()).replace(regEx, "+"));
return kvps.join('&');
};
$.ajax = function (o) {
var xhr = $.xhr(), timer, n = 0;
o = _extend({ userAgent: "XMLHttpRequest", lang: "en", type: "GET", data: null, dataType: "application/x-www-form-urlencoded" }, o);
if (o.timeout) timer = setTimeout(function () { xhr.abort(); if (o.timeoutFn) o.timeoutFn(o.url); }, o.timeout);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (timer) clearTimeout(timer);
if (xhr.status < 300) {
if (o.success) o.success($._xhrResp(xhr));
}
else if (o.error) o.error(xhr, xhr.status, xhr.statusText);
if (o.complete) o.complete(xhr, xhr.statusText);
}
else if (o.progress) o.progress(++n);
};
var url = o.url, data = null;
var isPost = o.type == "POST" || o.type == "PUT";
if (!isPost && o.data) url += "?" + $._formData(o.data);
xhr.open(o.type, url);
if (isPost) {
var isJson = o.dataType.indexOf("json") >= 0;
data = isJson ? JSON.stringify(o.data) : $._formData(o.data);
xhr.setRequestHeader("Content-Type", isJson ? "application/json" : "application/x-www-form-urlencoded");
}
xhr.send(data);
};
})(window.yournamespace);
@reillo
Copy link

reillo commented Oct 2, 2021

To use it:

Step1 :

Replace })(window.yournamespace); on the last line with your namespace. Example:

var awesome = {};
(function($) {
    var win=window, xhrs = [
      ...
    };
})(awesome);   

Step 2:

// look at jQuery's ajax documentation for possible parameters
// https://api.jquery.com/jquery.ajax/
awesome.ajax({
  ...
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment