1.0.0 • Published 8 years ago
charto-ajax v1.0.0
charto-ajax
Reinventing the wheel with another ajax library for browsers.
This one is really simple and compact.
- One function,
ajax. - Three parameters, only the first is mandatory:
- URL.
- Options object with
method,headersandbody, like the new fetch API.- Additionally, an
xhrfield for passing your ownXMLHttpRequestinstance. Useful if you need access for monitoring its progress. - TypeScript definition provided:
AjaxOptions.
- Additionally, an
- Object with query parameters. Keys are sorted and passed (with values) through
encodeURIComponent.
- Returns a
Promise. Resolves afteronloadwith status 200, rejects otherwise.- Either way, the value of the
Promiseis theXMLHttpRequestobject.
- Either way, the value of the
Usage
import { ajax } from 'charto-ajax';
ajax(
'https://www.google.com/',
{ method: 'GET' },
{ q: 'ajax' }
).then(
(xhr) => console.log(xhr.responseText)
);Why not use fetch?
- This is less code than a polyfill.
- The returned
Promiseis rejected on HTTP errors like 404. - Easy access to the
XMLHttpRequestobject during and after requests, which may be useful. - Query parameters are handled for you. Might save a bit of code, YMMV.
License
Copyright (c) 2017 BusFaster Ltd
1.0.0
8 years ago