A truly cross-browser and forward-compatible library to do asynchronous HTTP requests that follows the callback pattern.
npm install --save cb-fetch
yarn add cb-fetch
jspm install cb-fetch
bower install --save cb-fetch
<script src="//cdn.jsdelivr.net/combine/npm/@string/isstring/isString.min.js,npm/cb-fetch/index.min.js" type="text/javascript"></script>
define(function (require) {
var request = require('cb-fetch');
});
var request = require('cb-fetch')['default'];
var request = require('cb-fetch');
YUI({
modules: { 'is-string': 'path/to/@string/isstring.js' }
}).use('is-string', 'cb-fetch', function (Y) {
var request = Y['default'];
});
import request = require('cb-fetch');
If none of the previously listed module registration methods are supported, a global variable named request
will be exposed.
// here's your typical request
request('http://www.example.com?key1=value1&key2=value2')
.done(response => { /* … */ });
// taking a comprehensive approach is encouraged though
request()
.get('http://www.example.com')
.query('key1=value1&key2=value2')
.done(onSuccessCallback, onErrorCallback);
// passing an object offers options not available otherwise
let abort = request({
url: new URL('http://www.example.com'),
parameters: new URLSearchParams('_csrf=TOKEN'),
mode: 'cors',
credentials: 'include',
responseType: 'json'
}).get('/segment')
.query({ foo: ['bar', 'qux'] })
.pass('Content-Type', 'application/json')
.hook('download', e => { /* … */ })
.done({
success: onSuccessCallback,
error: onErrorCallback,
abort: onAbortCallback
});
// forcefully aborts the request
abort();
fetch
XMLHttpRequest
XDomainRequest
(Options.url?) => Object
(Options.parameters?) => Object
(Options.body?) => Object
('loadstart', () => Boolean | Void) => Object
('download', Object => Any) => Object
('loadend', () => Any) => Object
{
(name: String, value: String),
(headers: Object | Headers)
} => Object
// assigns
.pass(new Headers({ key: 'value' }))
// appends
.pass({ key: 'value' })
// sets
.pass('key', 'value')
{
(onSuccess?: Function, onError?: Function),
({
success?: Function,
error?: Function,
timeout?: Function,
abort?: Function
})
} => () => Void,
throws: TypeError
Property | Default | Value(s) |
---|---|---|
body | null | BufferSource, Blob, Document², FormData, String, URLSearchParams, ReadableStream |
credentials | 'same‑origin' | 'include', 'omit'⁶, 'same-origin' |
headers | {} | Object, Headers³ |
method | 'GET' | String |
mode | 'same‑origin' | 'cors', 'no-cors'¹, 'same-origin' |
password | null | String |
parameters | URLSearchParams, Object, String | |
responseMediaType² | String | |
responseType | 'text', 'json', 'blob', 'document', 'arraybuffer', 'formdata'¹, 'moz-blob', 'moz-chunked-arraybuffer', 'moz-chunked-text', 'msxml-document' | |
timeout | 0 | ℕ |
username | null | String |
url | location.href | String, URL |
multipart⁷ | false | Boolean |
tunneling⁵ | false | Boolean |
XSLPattern⁴ | false | Boolean |
Property | Type |
---|---|
chunk | String, ArrayBuffer, Blob, Uint8Array, null |
aggregate | String, ArrayBuffer, Blob, Uint8Array, null |
loaded | ℕ |
total | ℕ |
lengthComputable | Boolean |
Property | Type |
---|---|
body | Object, String, Document, ArrayBuffer, Blob, FormData¹, ReadableStream¹, null |
headers | Object |
instance | XMLHttpRequest, XDomainRequest, Response, AnonXMLHttpRequest |
statusCode | ℕ |
statusText | String |
url | String |
¹ fetch only
² XHR only
³ except Gecko 34–43
⁴ MSXML 3.0 only
⁵ method override
⁶ fetch, Gecko 16+, Presto/2.10.232–2.12.423
⁷ Gecko 1.7β–22
delete
reserved keywordIn pre-ES5 environments, the delete method requires the use of the bracket notation.
For the browsers powered by Gecko 1.9.1–20 to have the exposed response headers
populated into the headers
property, the following conditions must be met:
Access-Control-Expose-Headers
response header exposes itselfAccess-Control-Expose-Headers
field value is not *
mode
set to cors
XDomainRequest
intrinsic limitationsAccess-Control-Allow-Origin
header of either *
or the exact URL of the requesting documentInternet Explorer’s default settings restrict the use of 3rd party cookies unless a P3P compact policy
declaration has been included through a custom HTTP response header; hence, the "include"
credentials mode cannot be
fully honored if a cookie has been deemed unsatisfactory.