Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

ajax

> 编程语言
Open source

Ajax module in Vanilla JS

743 stars0 likes1 views
WebsiteGitHub

About

Ajax module in Vanilla JS

Ajax

Ajax module in Vanilla JS

Build Status Coveralls Coverage Status License CONTRIBUTING

You can use this module with AMD, CommonJS or just like a method of window object!

Installation

Bower

You can install via bower (but you should avoid that):

bash
bower install ajax

Manual installation

Just download dist/ajax.min.js file, and add dist/ajax.min.js on your HTML file:

xml

NPM

bash
npm i --save @fdaciuk/ajax

CDN

You may use a CDN to get the latest version.

CDNJS:

bash
https://cdnjs.com/libraries/fdaciuk-ajax

GitHub:

Or you may just add the following line to your HTML file:

xml

Usage

AMD

javascript
define(['ajax'], function (ajax) {
  ajax().get(...)
  ...
})

CommonJS

javascript
var ajax = require('@fdaciuk/ajax')
ajax().post(...)
...

ES6 / ES2015 module

javascript
import ajax from '@fdaciuk/ajax'
ajax().put(...)

Method of window object

javascript
window.ajax().get(...)

or just

javascript
ajax().get(...)

Signature

javascript
ajax([options])

Options

Optional object with request options. See all accepted options below.

HTTP Methods

You may pass any HTTP method as you want, using method property:

javascript
var request = ajax({
  method: 'options',
  url: '/api/users',
  data: {
    user: 'john'
  }
})

request.then(function (response) {...})

For using this kind of request, you must pass url property.

The property data is optional, but may used to pass any data via body on request.

headers

An object when key is a header name, and value is a header value.

javascript
ajax({
  headers: {
    'content-type': 'application/json',
    'x-access-token': '123@abc'
  }
})

If content-type is not passed, application/x-www-form-urlencoded will be used when you pass data as a query string.

Passing data as object, application/json will be automatically used (since v3.0.0).

Note about uploads:

If you need to upload some file, with FormData, use content-type: null.

baseUrl

You can pass a baseUrl param to improve calls. Example:

javascript
const request = ajax({ baseUrl: 'http://example.com/api/v2' })
request.get('/users') // get `http://example.com/api/v2/users` url

Methods

You may use any of this methods, instead the above approach:

get(url, [data])

Get data as a JSON object.

javascript
ajax().get('/api/users')

You can pass data on get method, that will be added on URL as query string:

javascript
ajax().get('/api/users', { id: 1 })

It will request on /api/users?id=1.

post(url, [data])

Save a new register or update part of this one.

javascript
// Without headers
ajax().post('/api/users', { slug: 'john' })

// With headers
var request = ajax({
  headers: {
    'x-access-token': '123@abc'
  }
})

request.post('/login', { username: 'user', password: 'b4d45$' })

data might be a complex object, like:

javascript
ajax().post('/api/new-post', {
  slug: 'my-new-post',
  meta: {
    categories: ['js', 'react'],
    tags: ['code']
  }
})

put(url, [data])

Update an entire register.

javascript
ajax().put('/api/users', { slug: 'john', age: 37 })

delete(url, [data])

Delete a register.

javascript
ajax().delete('/api/users', { id: 1 })

Return methods

Disclaimer: these return methods are not from real Promises, and they will just being called once. If you want to work with real Promises, you should make your own abstraction.

then(response, xhrObject)

Promise that returns if the request was successful.

javascript
ajax().get('/api/users').then(function (response, xhr) {
  // Do something
})

catch(responseError, xhrObject)

Promise that returns if the request has an error.

javascript
ajax().post('/api/users', { slug: 'john' }).catch(function (response, xhr) {
  // Do something
})

always(response, xhrObject)

That promise always returns, independent if the status is done or error.

javascript
ajax().post('/api/users', { slug: 'john' }).always(function (response, xhr) {
  // Do something
})

Abort request

If a request is very slow, you can abort it using abort() method:

javascript
const getLazyUser = ajax().get('/api/users/lazy')

const timer = setTimeout(function () {
  getLazyUser.abort()
}, 3000)

getLazyUser.then(function (response) {
  clearTimeout(timer)
  console.log(response)
})

In the above example, if request is slowest than 3 seconds, it will be aborted.

Deprecated methods

You may see the deprecated methods here

Contributing

Check CONTRIBUTING.md

Code coverage and Statistics

Browser compatibility

Chrome Firefox IE Edge Opera Safari
Latest ✔ Latest ✔ 9+ ✔ Latest ✔ Latest ✔ 3.2+ ✔

License

MIT © Fernando Daciuk

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScripthacktoberfest

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言