ajax.js intends to be a nice, lightweight, AJAX library. Unminified the library is 3kb, minification takes it down to ~2kb.
Download the minified release here.
ajax.js supports CommonJS/AMD and will fall back to declaring a single global ajax function.
We support all evergreen browsers, and tentatively IE 10+.
ajax("http://url.com").send(function(data, status, headers) {
console.log(data);
});
notes:
data
will be parsed as json..send()
bundles everything up and preforms the request.var promise = ajax("http://url.com").send();
promise.then(function(data) {
console.log(data);
});
notes:
ajax("http://picture.org").raw().send(callback(data, status, headers));
notes:
.raw()
will disable JSON parsing and simply pass the raw data to the callback. ajax("http://postingvars.name", "post").vars({'name':'bob'}).data({'request':'body data'}).send(callback(data, status, headers));
notes:
ajax()
takes any valid HTTP method as it's second argument..vars()
can naturally be used for any request type, it simply generates URL variables from a valid JS object: {key:value}
becomes ?key=value
(and appended to the URL)..data()
takes an object and generates a urlencoded string to be sent in the request body data. This is only valid for POST and PUT request types, GET will simply discard the data.ajax("https://thismightfail.org")
.progress(function(e) { console.log("progress!"); })
.error(function(e) { console.log("*BOOM*"); })
.send(callback(data, status, headers));
notes:
ajax("http://a_domain.pirate").headers({"X-SAIL":"7-seas"}).send(callback(data, status, headers));
notes:
ajax('url', 'get|post|put|delete')
.vars({'some':'variables'})
.data({'more':'variables'})
.raw(true|false)
.headers({'req':'headers'})
.progress(function(event))
.error(function(event))
.send(callback(data, status, headers));
//optionally .then(function(data), function(error));
// can be appended to .send() when using the Promise
// form (invoking send with no callback)
ajax("http://url.com", "get")
The first argument must be a well formed URL, the second argument optionally specifies the HTTP request method (defaults to GET). If no arguments are provided a GET request to the current page will be constructed.
.send(function(data, status, headers));
Sends the request and either invokes the function passed as a callback on completion, or if no callback function is provided, it returns a Promise. Be sure to polyfill Promises if you need support in older browsers.
The callback will be supplied with the response data
, the resulting status
code, and string of the received headers
(an empty string on platforms that don't support getting response headers).
.vars({'one':'fish', 'two':'fish', 'red':'fish', 'blue','fish'})
Expects an object as it's only parameter. It will unpack the object into a argument string and append that to the request URL.
.data({'one':'var', 'another':'var'})
Unpacks the object into the body of a POST request, and sets the appropriate headers (namely Content-type: application/x-www-form-urlencoded).
.raw()
When used with no parameters it disables JSON parsing on the request data. When given a boolean it will enable (false) or disable (true) JSON parsing.
.headers({'x-some-header':'some-value'})
Expects an object as it's only parameter. The unpacking of the header object is delayed until the request has being preformed due to the requirements of XMLHttpRequest, namely that headers can only be appended after open() has been called.
.progress(function(e){ //handle event })
Binds the given function as the progress event handler, the callback will receive ProgressEvents - See this for more detail.
.error(function(e){ //handle event })
Binds the given function as the error event handler, the callback will receive ProgressEvents - See this for more detail.