140bytes abstraction for a versatile AJAX request (both sync and async, get/post/head, optionally with user/password).
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}
Shiny. Nice use of XMLHttpRequest.open shorthand -- I hadn't seen that before.