req-ajax

Standalone library for ajax requests.
Installation
Execute this command in your project for install the package:
npm install --save req-ajax
After the download you should install the dependencies and build es2015 version with babel.js, so execute this:
cd node_modules/req-ajax; npm run build; cd -
The file ajax.js
and ajax.min.js
are in dist
directory.
Usage
Simple external script in your html file:
<script src="node_modules/req-ajax/dist/ajax.min.js"></script>
API
All methods return a Promise
object.
Methods
ajax.query(Object)
the Object is the config of that request, this object can have 3 properties:
url
{String} is required.method
{String} is required.params
{Object} is optional, this property is for specific queries. The queries like this foo=bar&bar=foo
can be transformed to { foo: 'bar', bar: 'foo' }
in the JavaScript object notation, so is much better for make queries and easy.
Example:
ajax.query({
url: 'somepath',
method: 'GET',
params: {
foo: 'bar'
}
}).then(function (res) {
}).catch(function (err) {
})
ajax.get(url [, json])
This method is an shorthand for GET requests.
Params:
url
{String} is required.json
{Boolean} is optional, default false
. This param is for when the response is json string, if json
is true
the response is parser for convert it to a object normal.
Per Example the file somefile.json
contain { "foo": "bar" }
:
ajax.get('somefile.json', true)
.then(function (res) {
if (res.foo === 'bar') {
}
}).catch(function (err) {
})
ajax.post(url, data [, json])
This method is an shorthand for POST requests.
Params:
url
{String} is required.data
{String|Number|Object|Boolean} is required.json
{Boolean} is optional, default false
. This param is for when the data is object normal and will send it as a json string.
Example:
const data = { foo: 'bar' }
ajax.post('somepath', data, true)
.then(function ()
}).catch(function (err) {
})
For more examples and test execute npm run test
and open your browser in the url http://localhost:8080
.