UNPKG

unjq-ajax

Version:

AJAX library abstracted from jQuery offers the same APIs.

135 lines (99 loc) 2.89 kB
# UnJQ-Ajax An AJAX library abstracted from jQuery offers the same APIs as jQuery AJAX without other redundancy. ## Installation npm install unjq-ajax ---------------- ## Features #### Ajax methods in jQuery style such as: .ajax, .get, .post, .getJSON, .getScript #### Linkable event-handlers such as: .then(func), .done(func), .fail(func), .always(func) #### Global events are NOT supported such as: ajaxStart, ajaxSend, ajaxSuccess, ajaxError, ajaxComplete, ajaxStop ### Practical methods #### serialize ( form ) Encode a set of form elements as a string for submission. - @form: FORM HTMLElement #### serializeArray ( form ) Encode a set of form elements as an array of names and values. - @form: FORM HTMLElement #### Unchanged methods in jQuery style such as isFunction, isWindow, isPlainObject, extend, param #### upload ( fileElements , url [, data ] [, success] [, dataType ] ) 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' ``` #### GET ``` UnJQ.get('/json' , { memo: 'Hello World' } , (resp) => { //TODO }, 'json') .done(() => { //TODO }) .fail(() => { //TODO }) .always(() => { //TODO }) ``` #### getJSON ``` UnJQ.getJSON('/json' , (resp) => { //TODO }) .fail(() => { //TODO }) ``` #### getScript ``` 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." ); }) ``` #### POST & Form Serialization ``` UnJQ.post('/post' , serialize( document.getElementById('form1') ) , (resp) => { //TODO }, 'json') ``` #### Simple File Upload ``` let file = document.getElementById('theFile') upload(file, '/post' , serialize( document.getElementById('form1') ) , (resp) => { //TODO }) ``` ---------------- ## Run Demo Run the server ``` node examples/server.js ``` DEMO Links - http://localhost:3000 ---------------- ##License MIT (http://www.opensource.org/licenses/mit-license.php)