Skip to content

Instantly share code, notes, and snippets.

@aretecode
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aretecode/d558abb1397e16af1302 to your computer and use it in GitHub Desktop.
Save aretecode/d558abb1397e16af1302 to your computer and use it in GitHub Desktop.

Smallest & fastest ajax library with differentiations according to the use case.

This is the (slightly) stripped down version of the https://github.com/aretecode/aj

/**
*
* @param {} d .cb = callback, .m = method, .u = url, .d = data, .ct = contenttype
*
*/
function aj(d) {
// make a XMLHttpRequest, hope it is not an old IE & FF browser
var request = new XMLHttpRequest();
// pass in a callback
request.onreadystatechange = d.cb;
// if the method is GET, add a `?` & the data
request.open(d.m, d.u + (d.m == "GET" ? "?" + d.d : "") , true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// use the contenttype
request.setRequestHeader('Content-type', d.ct);
// send the data
request.send(d.d);
}
function aj(e){var t=new XMLHttpRequest;t.onreadystatechange=e.cb,t.open(e.m,e.u+("GET"==e.m?"?"+e.d:""),!0),t.setRequestHeader("X-Requested-With","XMLHttpRequest"),t.setRequestHeader("Content-type",e.ct),t.send(e.d)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 aretecode
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "aj",
"description": "Smallest & fastest ajax library with differentiations according to the use case.",
"keywords": [
"ajax",
"javascript"
"request",
"callback",
"http"
]
}
<!DOCTYPE html>
<title>Aj(ax)</title>
<div>Expected value: <b>0 then 404</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
function aj(e){var t=new XMLHttpRequest;t.onreadystatechange=e.cb,t.open(e.m,e.u+("GET"==e.m?"?"+e.d:""),!0),t.setRequestHeader("X-Requested-With","XMLHttpRequest"),t.setRequestHeader("Content-type",e.ct),t.send(e.d)}
var test = function() {
aj(
{
u: "url.php",
m: "GET",
ct: "application/x-www-form-urlencoded",
cb: function(response){document.getElementById("ret").innerHTML = response.target.status },
d: "test=true"
}
);
}
test();
</script>
@atk
Copy link

atk commented Jun 23, 2015

Alas, that's far more than 140bytes (217). Let's golf!

@atk
Copy link

atk commented Jun 23, 2015

Here's a reduced version in 139 bytes:

function aj(e,t,i){t=new XMLHttpRequest;t.onreadystatechange=e.cb;t.open(e.m,e.u,!0);for(i in e.h)t.setRequestHeader(i,e.h[i]);t.send(e.d)}

e = {
   m: 'post',
   u: "url.php?get=parameters",
   h: { "Content-Type": "application/x-www-form-urlencoded", "X-Requested-With": "XMLHttpRequest" },
   d: "data=true",
   cb: function() { /* callback */ }
};

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