Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Last active February 1, 2018 13:56
Show Gist options
  • Save atk/6620726 to your computer and use it in GitHub Desktop.
Save atk/6620726 to your computer and use it in GitHub Desktop.
AJAX abstraction

AJAX abstraction

140bytes abstraction for a versatile AJAX request (both sync and async, get/post/head, optionally with user/password).

Usage

The AJAX request abstraction requires one option object that must contain at least the url attribute:

ajax_req({ url: '...' })

The following attributes can change the behaviour of the call:

{
   url: '/request/url',
   method: 'post', // get by default, head and post possible
   callback: function(){ ... }, // if defined, request will be async; called on readyStateChange
   data: 'test1=1&test2=2', // POST data
   user: 'username',
   pass: 'password'
}

Calling the request will return the XMLHttpRequest object for further manipulation.

Using the "with" keyword, it is possible to save one more byte. However, this is rather ugly and therefore discouraged for productive use:

function(o,x){with(x=new XMLHttpRequest())open(o.method||'get',o.url,(onreadystatechange=o.callback),o.user,o.pass),send(o.data);return x}
function(
o, // options
x // Request Object
){
// create new Request Object
x = new XMLHttpRequest(),
// open/initialize with options, set onreadystatechange if async
x.open(o.method || 'get', o.url, (x.onreadystatechange = o.callback), o.user, o.pass),
// send Request
x.send(o.data);
// return Request Object for later use
return x
}
function(o,x){x=new XMLHttpRequest(),x.open(o.method||'get',o.url,(x.onreadystatechange=o.callback),o.user,o.pass),x.send(o.data);return x}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexthkloss@web.de>
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": "AJAX abstraction",
"description": "Provides a convenient abstraction for an AJAX request",
"keywords": [
"ajax",
"request",
"networking",
"abstraction"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>true</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(o,x){x=new XMLHttpRequest(),x.open(o.method||'get',o.url,(x.onreadystatechange=o.callback),o.user,o.pass),x.send(o.data);return x}
document.getElementById( "ret" ).innerHTML = /AJAX/.test(myFunction({url:''}).responseText)
</script>
@jwoglom
Copy link

jwoglom commented May 8, 2014

Shiny. Nice use of XMLHttpRequest.open shorthand -- I hadn't seen that before.

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