tot-ajax
v2.0.0
Published
Minimalistic browser script for ajax.
Readme
tot-ajax
Minimalistic browser script for ajax.
Usage
ajax(url, [async=true], [user], [password])
.get([data], [callback(err, res, xhr, ajaxObj)])
.then( (res, xhr, ajaxObj) => { ... } )- Call to
ajaxfunction returns anajaxObjwrapper object fornew XMLHttpRequest(). - Wrapper properties:
method,url,async,user,password,xhr,response. - Wrapper methods:
run,get,post,put,delete. runsends thexhrwithmethod; other methods just setmethodand callrun.- Methods accept optional callback and return a promise.
- If data passed to a method is an object (except FormData) then it gets automatically stringified and sent with
'Content-Type: application/json'header. - If response has
'Content-Type: application/json'header then it gets automatically parsed.
Examples
Say we want to update Jayson's email and imagine the server sends the whole updated user object in the response.
ajax('/users?name=Jayson')
.post({email: '[email protected]'})
.then( user => {
console.log(user.name + "'s email is " + user.email)
})Installation
npm install tot-ajax
Advanced installation
This command will produce a single .js file in the current working directory:
Linux: _npm_postinstall_args=justmain npm i tot-ajax
Windows: set _npm_postinstall_args=justmain & npm i tot-ajax
- The error after installing this way is a trick to make npm safely remove module directory.
- On Windows
_npm_postinstall_argswon't be reset after command execution, so if you want to install the module normally after installing it "advanced" way then you should restart the shell or clear_npm_postinstall_argsmanually.
