Ajax module in Vanilla JS
You can use this module with AMD, CommonJS or just like a method of
You can install via bower (but you should avoid that):
bower install ajax
dist/ajax.min.js file, and add
dist/ajax.min.js on your HTML file:
npm i --save @fdaciuk/ajax
You may use a CDN to get the latest version.
Or you may just add the following line to your HTML file:
var ajax =...
ES6 / ES2015 module
Optional object with request options. See all accepted options below.
You may pass any HTTP method as you want, using
var request =request
For using this kind of request, you must pass
data is optional, but may used to pass any data via
body on request.
An object when
key is a header name, and
value is a header value.
content-type is not passed,
application/x-www-form-urlencoded will be used when you pass
data as a query string.
application/json will be automatically used (since v3.0.0).
Note about uploads:
If you need to upload some file, with
You can pass a
baseUrl param to improve calls. Example:
const request =request // get `` url
You may use any of this methods, instead the above approach:
Get data as a JSON object.
You can pass
get method, that will be added on URL as query string:
It will request on
Save a new register or update part of this one.
// Without headers// With headersvar request =request
data might be a complex object, like:
Update an entire register.
Delete a register.
Disclaimer: these return methods are not from real Promises, and they will just being called once. If you want to work with real Promises, you should make your own abstraction.
Promise that returns if the request was successful.
Promise that returns if the request has an error.
That promise always returns, independent if the status is
If a request is very slow, you can abort it using
const getLazyUser =const timer =getLazyUser
In the above example, if request is slowest than 3 seconds, it will be aborted.
You may see the deprecated methods here
Code coverage and Statistics
|Latest ✔||Latest ✔||9+ ✔||Latest ✔||Latest ✔||3.2+ ✔|
MIT © Fernando Daciuk