/ajax

Ajax module in Vanilla JS

Primary LanguageJavaScriptOtherNOASSERTION

Ajax

Ajax module in Vanilla JS

Ajax

Build Status Coveralls Coverage Status Code Climate Coverage Code Climate License CONTRIBUTING

Usage

You can install via bower:

bower install ajax

or add dist/ajax.min.js on your HTML:

<script src="js/ajax.min.js"></script>

You can use this module with AMD, CommonJS or just like a method of window object!

AMD

define([ 'Ajax' ], function( Ajax ) {
  var ajax = new Ajax();
  ...
});

CommonJS

var Ajax = require( './js/ajax.min' );
var ajax = new Ajax();
...

Method of window object

var ajax = new window.Ajax();

or

var ajax = new Ajax();

Enjoy ;)

Methods

get(url)

Get data as a JSON object.

var ajax = new Ajax();
ajax.get( '/api/users' );

post(url, [ data ])

Save a new register.

var ajax = new Ajax();
ajax.post( '/api/users', { data: 'value' });

put(url, [ data ])

Upgrade part of a register.

var ajax = new Ajax();
ajax.put( '/api/users', { slug: 'john' });

delete(url, [ data ])

Delete a register.

var ajax = new Ajax();
ajax.delete( '/api/users', { id: 1 });

Return methods

done(response, xhrObject)

Promise that returns if the request was successful.

var ajax = new Ajax();
ajax.get( '/api/users' ).done(function( response, xhr ) {
  // Do something
});

error(response, xhrObject)

Promise that returns if the request has an error.

var ajax = new Ajax();
ajax.post( '/api/users', { slug: 'john' }).error(function( response, xhr ) {
  // Do something
});

always(response, xhrObject)

That promise always returns, independent if the status is done or error.

var ajax = new Ajax();
ajax.post( '/api/users', { slug: 'john' }).always(function( response, xhr ) {
  // Do something
});

Contributing

Check CONTRIBUTING.md

Code coverage and Statistics

https://github.com/reportz/ajax

License

MIT © Fernando Daciuk