All Projects → codex-team → ajax

codex-team / ajax

Licence: MIT license
Just another AJAX requests helper

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ajax

Vue File Upload
vue.js ,vue-loader 上传文件,vue-file-upload,vue上传文件组件
Stars: ✭ 317 (+1074.07%)
Mutual labels:  file-upload, uploader
Uploadcare Php
PHP API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
Stars: ✭ 77 (+185.19%)
Mutual labels:  file-upload, uploader
Dropit
DropIt is a File Uploader built with nodejs, Upload, get a link, and share your files with anyone easily.
Stars: ✭ 367 (+1259.26%)
Mutual labels:  file-upload, uploader
uploadcare client
A flutter library for working with Uploadcare REST API. File uploads, media processing, and adaptive delivery for web and mobile.
Stars: ✭ 14 (-48.15%)
Mutual labels:  file-upload, uploader
Sharex Upload Server
AKA ShareS - Feature full & Stable ShareX and file server in node. Includes images, videos, code, text, markdown rendering, password protected uploads, logging via discord, administration through Discord, url shortening, and a full front end. Use standalone or via reverse proxy
Stars: ✭ 180 (+566.67%)
Mutual labels:  file-upload, uploader
Ajaxfileupload
A jQuery plugin that simulates asynchronous file uploads.
Stars: ✭ 291 (+977.78%)
Mutual labels:  file-upload, ajax
Tus Php
🚀 A pure PHP server and client for the tus resumable upload protocol v1.0.0
Stars: ✭ 1,048 (+3781.48%)
Mutual labels:  file-upload, uploader
React Dropzone Uploader
React file dropzone and uploader
Stars: ✭ 276 (+922.22%)
Mutual labels:  file-upload, uploader
Filestack Js
Official Javascript SDK for the Filestack API and content ingestion system.
Stars: ✭ 169 (+525.93%)
Mutual labels:  file-upload, uploader
Uppload
📁 JavaScript image uploader and editor, no backend required
Stars: ✭ 1,673 (+6096.3%)
Mutual labels:  file-upload, uploader
lolisafe
Blazing fast file uploader and awesome bunker written in node! 🚀
Stars: ✭ 181 (+570.37%)
Mutual labels:  file-upload, uploader
V Uploader
A Vue2 plugin make files upload simple and easier, single file upload with image preview, multiple upload with drag and drop
Stars: ✭ 216 (+700%)
Mutual labels:  file-upload, uploader
Chibisafe
Blazing fast file uploader and awesome bunker written in node! 🚀
Stars: ✭ 657 (+2333.33%)
Mutual labels:  file-upload, uploader
Angular File Uploader
Angular file uploader is an Angular 2/4/5/6/7/8/9/10 + file uploader module with Real-Time Progress Bar, Responsive design, Angular Universal Compatibility, localization and multiple themes which includes Drag and Drop and much more.
Stars: ✭ 92 (+240.74%)
Mutual labels:  file-upload, uploader
Uploadcare Widget
Uploadcare Widget, an ultimate tool for HTML5 file upload supporting multiple file upload, drag&drop, validation by file size/file extension/MIME file type, progress bar for file uploads, image preview.
Stars: ✭ 183 (+577.78%)
Mutual labels:  file-upload, uploader
react-native-tus-client
React Native client for the tus resumable upload protocol.
Stars: ✭ 38 (+40.74%)
Mutual labels:  file-upload, uploader
cpomf
Pomf API compatible file host written in Crystal - The software behind nya.is.
Stars: ✭ 36 (+33.33%)
Mutual labels:  file-upload
front-end-notes
前端课程学习笔记汇总
Stars: ✭ 57 (+111.11%)
Mutual labels:  ajax
sekoliko
Sekoliko | MySchool | MonEcole : School management Software.
Stars: ✭ 39 (+44.44%)
Mutual labels:  ajax
AjaxHandler
ASimple PHP Class to help handling Ajax Requests easily
Stars: ✭ 30 (+11.11%)
Mutual labels:  ajax

AJAX

Module for async requests on a native JavaScript for a browser.

Package has been renamed from codex.ajax to @codexteam/ajax

Features

  • zero-dependencies
  • Promises based
  • custom callback for a progress event
  • easy-to-use transport method: ask user for a file(s) and upload it
  • object, FormData or HTMLFormElement data is being supported

Installation

You can install this package via NPM or Yarn

npm install @codexteam/ajax
yarn add @codexteam/ajax

Require package on your script page.

const ajax = require('@codexteam/ajax');

Also you can get this module from CDN or download a bundle file and use it locally.

Usage

There are a few public functions available to be used by user. All of them return Promise.

Callbacks format

successCallback and errorCallback have the same input object response as a param.

param type description
response.body object or string Response body parsed JSON or a string
response.code number Response code
response.headers object Response headers object

Example

function successCallback(response) {
  console.log('Response:', response.body);
}
function errorCallback(response) {
  console.log(`Error code ${response.code}. Response:`, response.body);
}

ajax.get()

Wrapper for a GET request over an ajax.request() function.

param type default value description
url string '' Request URL
data object null Data to be sent
headers object null Custom headers object
progress function (percentage) => {} Progress callback
ratio number 90 Max % of bar for uploading progress
beforeSend function null Fire callback before sending a request

Example

ajax.get({
 url: '/getUserData',
 data: {
   user: 22
 }
})
  .then(successCallback)
  .catch(errorCallback);

ajax.post()

Wrapper for a POST request over an ajax.request() function.

param type default value description
url string '' Request URL
data object, FormData or HTMLFormElement null Data to be sent
type string ajax.contentType.JSON Header from ajax.contentType object
headers object null Custom headers object
progress function (percentage) => {} Progress callback
ratio number 90 Max % of bar for uploading progress
beforeSend function null Fire callback before sending a request

Example

Simple POST request

ajax.post({
  url: '/saveArticle',
  data: {
    title: 'Awesome article',
    text: 'will be written later',
    isPublished: false
  },
  
  /**
   * Choose the content type you need
   */
  // type: ajax.contentType.JSON /* (default) */ 
  // type: ajax.contentType.URLENCODED
  // type: ajax.contentType.FORM
})
  .then(successCallback)
  .catch(errorCallback);

Example

To send any form you can pass HTMLFormElement as a data to ajax.post().

<form id="form-element">
    <input type="text" name="firstName" placeholder="First name">
    <input type="text" name="lastName" placeholder="Last name">
    <input type="file" name="profileImage" accept="image/*">
    <button onclick="event.preventDefault(); sendForm()">Send form</button>
</form>

<script>
function sendForm() {
  var form = document.getElementById('form-element');
  
  ajax.post({
    url:'/addUser',
    data: form
  })
    .then(successCallback)
    .catch(errorCallback);
} 
</script>

ajax.request()

Main function for all requests.

param type default value description
url string '' Request URL
method string 'GET' Request method
data object null Data to be sent
headers object null Custom headers object
progress function (percentage) => {} Progress callback
ratio number 90 Max % of bar for uploading progress
beforeSend function (files) => {} Fire callback before sending a request

Example

ajax.request({
  url: '/joinSurvey',
  method: 'POST',
  data: {
    user: 22
  }
})
  .then(successCallback)
  .catch(errorCallback);

ajax.transport()

This is a function for uploading files from client.

User will be asked to choose a file (or multiple) to be uploaded. Then FormData object will be sent to the server via ajax.post() function.

param type default value description
url string '' Request URL
data object null Additional data to be sent
accept string null Mime-types of accepted files
multiple boolean false Let user choose more than one file
fieldName string 'files' Name of field in form with files
headers object null Custom headers object
progress function (percentage) => {} Progress callback
ratio number 90 Max % of bar for uploading progress
beforeSend function (files) => {} Fire callback with chosen files before sending

Example

ajax.transport({
  url: '/uploadImage',
  accept: 'image/*',
  progress: function (percentage) {
    document.title = `${percentage}%`;
  },
  ratio: 95,
  fieldName: 'image'
})
  .then(successCallback)
  .catch(errorCallback);

Example

One simple button for uploading files.

<button onclick='ajax.transport({url: "/uploadFiles"}).then(successCallback).catch(errorCallback)'>Upload file<button>

ajax.selectFiles()

Ask user for a file (or multiple) and process it. FileList object will be returned in Promise.

param type default value description
accept string null Mime-types of accepted files
multiple boolean false Let user choose more than one file

Example

ajax.selectFiles({
  accept: 'image/*'
})
  .then(successCallback);

Params

List of params, their types, descriptions and examples.

url string

Target page URL. By default current page url will be used.

/user/22, /getPage, /saveArticle

method string

Used in ajax.request() function only

Request method.

GET, POST

Read more about available request methods methods on the page at developer.mozilla.org.

data object|FormData|HTMLFormElement

You can pass data as object, FormData or HTMLFormElement.

Data will be encoded automatically.

ajax.request({
  url: '/joinSurvey',
  method: 'POST',
  data: {user: 22}
})
  .then(successCallback)
  .catch(errorCallback);
ajax.request({
  url: '/sendForm',
  method: 'POST',
  data: new FormData(document.getElementById('my-form'))
})
  .then(successCallback)
  .catch(errorCallback);

For ajax.get() you can pass object data

ajax.get({
  url: '/getUserData',
  data: {
    user: 22
  }
})
  .then(successCallback)
  .catch(errorCallback);

is the same as

ajax.get({
  url: '/getUserData?user=22'
})
  .then(successCallback)
  .catch(errorCallback);

For ajax.transport() should pass object data if it is necessary

You can send additional data with files.

ajax.transport({
  url: '/uploadImage',
  accept: 'image/*',
  data: {
    visible: true,
    caption: 'Amazing pic'
  },
  fieldName: 'image'
})
  .then(successCallback)
  .catch(errorCallback);

type string

Specify the content type of data to be encoded (by ajax module) and sent.

You can get value for this param from ajax.contentType object. Data will be encoded that way.

ajax.contentType value
JSON application/json; charset=utf-8
URLENCODED application/x-www-form-urlencoded; charset=utf-8
FORM multipart/form-data
const params = {
  // ...
  
  type: ajax.contentType.JSON 
  // type: ajax.contentType.URLENCODED
  // type: ajax.contentType.FORM
};

headers object

Object of custom headers which will be added to request.

headers = {
  'authorization': 'Bearer eyJhbGciJ9...TJVA95OrM7h7HgQ',
  // ...
}

progress function

Almost all requests have responses. To show a correct progress for a call we need to combine a request progress (uploading) and a response progress (downloading). This ajax module uses one progress callback for it.

/**
 * @param {number} percentage - progress value from 0 to 100 
 */
var progressCallback = function progressCallback(percentage) {  
    document.title = `${percentage}%`;
};

Check out ratio param to show progress more accurate.

ratio number

Used with progress param

Value should be in the 0-100 interval.

If you know that some requests may take more time than their responses or vice versa, you can set up a ratio param and define a boundary between them on the progress bar.

For example if you want to show progress for a file uploading process, you know that uploading will take a much more time than downloading response, then pass bigger ratio (~95). When you want to download big file — use smaller ratio (~5).

accept string

Used in ajax.transport() function only

String of available types of files to be chosen by user.

*/* — any files (default)

image/* — only images

image/png, image/jpg, image/bmp — restrict accepted types

Read more about MIME-types on the page at developer.mozilla.org.

multiple boolean

Used in ajax.transport() function only

false by default. User can choose only one file.

If you want to allow user choose more than a one file to be uploaded, then pass a true value.

fieldName string

Used in ajax.transport() function only

Name of data field with the file or array of files.

files by default.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].