axjs
v0.4.1
Published
A microscopic, RESTful, AJAX browser library; just under 2kb.
Maintainers
Readme

A microscopic, RESTful, AJAX browser library; just under 2kb.
Installation:
Ax
var request = new Ax( Object || Arguments); Arguments Method
var request = new Ax(method, url, [data], [type], done);Arguments
- method (String): http request (e.g., GET, PUT, POST, PATCH, DELETE, etc...)
- url (String): location/href
- [data] (Object|JSON): required if uploading data to the server
- [type] (String): Content-Type header (default is application/json)
- done (Function) Callback when request is finished, is always the last argument passed.
Object Method
Similar approach, see arguments above for explanation of values...
var request = new Ax({
method: method,
url: url,
data: [data],
type: [type],
done: done
}, [done]);NOTE: the done callback can either be in the first argument that is an object, or the last argument; but not both. If a function is detected as the last argument, Ax will use that and ignore the other.
Callback
The done callback function is passed as the last argument.
var request = new Ax(...args, function (res, xhr, err) {
// Single error detection
if (err) {
// Whoops lets do something
}
console.log('response is', res);
});It may also be part of the options object
var request = new Ax({
...options,
done: function (res, xhr, err) {
if (err) console.error(err);
console.log(res, xhr);
}
});Examples
Get user data...
var request = new Ax('GET', '/users/' + userid', function (res, xhr, err) {
if (err) {
// An error has occurred
} else {
var userdata = res;
// Do something with user data.
}
});Update a user profile...
var request = new Ax({
method: 'PUT',
url: userUrl,
data: newData,,
done: callBack()
});Post a form...
var request = new Ax('POST', '/submit/', formData, function (res, xhr, err) {
if (err) {
alert ('total failure', err);
} else {
console.log('server response is', res);
}
});Browser
<script src="ax.min"></script>NodeJS
Ax.js has been built with browserify, and works with both browserify and webpack.
Install via npm:
npm install axjsrequire the module
var Ax = require('axjs');CommonJS
Ax.js can be required in with a UMD or AMD library like requirejs.
var Ax = require('ax');