Ajax module in Vanilla JS
You can use this module with AMD, CommonJS or just like a method of window
object!
You can install via bower:
bower install ajax
Just download dist/ajax.min.js
file, and add dist/ajax.min.js
on your HTML file:
<script src="js/ajax.min.js"></script>
npm i --save @fdaciuk/ajax
You can just add the following line to your HTML file:
<script src="https://cdnjs.cloudflare.com/ajax/libs/fdaciuk-ajax/0.0.12/ajax.min.js"></script>
define([ 'Ajax' ], function( Ajax ) {
var ajax = new Ajax();
...
});
var Ajax = require( '@fdaciuk/ajax' );
var ajax = new Ajax();
...
var ajax = new window.Ajax();
or
var ajax = new Ajax();
Enjoy ;)
Get data as a JSON object.
var ajax = new Ajax();
ajax.get( '/api/users' );
ajax.get( '/api/users/john' );
Save a new register or update part of this one.
var ajax = new Ajax();
ajax.post( '/api/users', { slug: 'john' });
Update an entire register.
var ajax = new Ajax();
ajax.put( '/api/users', { slug: 'john', age: 37 });
Delete a register.
var ajax = new Ajax();
ajax.delete( '/api/users', { id: 1 });
Promise that returns if the request was successful.
var ajax = new Ajax();
ajax.get( '/api/users' ).done(function( response, xhr ) {
// Do something
});
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
});
That promise always returns, independent if the status is
done
orerror
.
var ajax = new Ajax();
ajax.post( '/api/users', { slug: 'john' }).always(function( response, xhr ) {
// Do something
});
Check CONTRIBUTING.md
https://github.com/reportz/ajax
MIT © Fernando Daciuk