unjq-ajax
Version:
AJAX library abstracted from jQuery offers the same APIs.
135 lines (99 loc) • 2.89 kB
Markdown
from jQuery offers the same APIs as jQuery AJAX without other redundancy.
npm install unjq-ajax
----------------
such as: .ajax, .get, .post, .getJSON, .getScript
such as: .then(func), .done(func), .fail(func), .always(func)
such as: ajaxStart, ajaxSend, ajaxSuccess, ajaxError, ajaxComplete, ajaxStop
Encode a set of form elements as a string for submission.
- @form: FORM HTMLElement
Encode a set of form elements as an array of names and values.
- @form: FORM HTMLElement
such as isFunction, isWindow, isPlainObject, extend, param
Upload file(s) with one or several file input elements.
- @fileElements: single of array of File Input HTMLElements.
- @url: A string containing the URL to which the request is sent.
- @data: A plain object or string that is sent to the server with the request.
- @success: A callback function that is executed if the request succeeds. Required if dataType is provided, but you can use null as a placeholder.
- @dataType: The type of data expected from the server. Default: Intelligent Guess (xml, json, script, or html).
----------------
## Examples
#### import modules
```
import UnJQ, {serialize, upload} from 'unjq-ajax'
```
```
UnJQ.get('/json'
, { memo: 'Hello World' }
, (resp) => {
//TODO
}, 'json')
.done(() => {
//TODO
})
.fail(() => {
//TODO
})
.always(() => {
//TODO
})
```
```
UnJQ.getJSON('/json'
, (resp) => {
//TODO
})
.fail(() => {
//TODO
})
```
```
UnJQ.getScript( "test.js"
, ( data, textStatus, jqxhr ) => {
console.log( data ); // Data returned
console.log( textStatus ); // Success
console.log( jqxhr.status ); // 200
console.log( "Load was performed." );
})
```
```
UnJQ.post('/post'
, serialize( document.getElementById('form1') )
, (resp) => {
//TODO
}, 'json')
```
```
let file = document.getElementById('theFile')
upload(file, '/post'
, serialize( document.getElementById('form1') )
, (resp) => {
//TODO
})
```
----------------
Run the server
```
node examples/server.js
```
DEMO Links
- http://localhost:3000
----------------
MIT (http://www.opensource.org/licenses/mit-license.php)
An AJAX library abstracted