# 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 `ajax` function returns an `ajaxObj` wrapper object for `new XMLHttpRequest()`.
- Wrapper properties: `method`, `url`, `async`, `user`, `password`, `xhr`, `response`.
- Wrapper methods: `run`, `get`, `post`, `put`, `delete`.
- `run` sends the `xhr` with `method`; other methods just set `method` and call `run`.
- 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: 'jayson_parser@mail.com'})
	.then( user => {
		console.log(user.name + "'s email is " + user.email)
	})
```


## Installation
`npm install tot-ajax`

**Advanced installation**<br/>
This command will produce a single `.js` file in the current working directory:<br/>
Linux: `_npm_postinstall_args=justmain npm i tot-ajax`<br/>
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_args` won'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_args` manually.
