RGJS6 XHR module.
Members
-
<inner, constant> _options :object
-
Default options for
get(),getJson(),post(), etc.Type:
- object
Properties:
Name Type Description idstring Identifier for request. Used to abort ongoing requests.
Generated automatially if not set. Ongoing request with matching id will be aborted.cacheboolean Use cache. Defaults to
false.cacheMaxAgeMsnumber Max age of cached data in milliseconds. Defaults to 1 hr.
mimeTypestring Mime type for get. Defaults to
'text/html'.dataTypestring Data type for post. Defaults to
'application/json'.headersarray Headers for the request. Defaults to
null.
Methods
-
<static> abort(requestId [, cleanUp])
-
Abort a request.
Parameters:
Name Type Argument Default Description requestIdstring The request id returned by getJson(), post(), etc.
cleanUpboolean <optional>
true Also cleans up request if true. Defaults to true.
Returns:
True if the request was aborted & cleaned up, false if it wasn't found.
- Type
- boolean
Example
// Perform a request, then abort it. const request_id = rgjs.xhr.getJson('//example.com/api/', callback, error); rgjs.xhr.abort(request_id); -
<static> abortAll()
-
Abort all requests.
Example
// Perform two requests, then abort both. rgjs.xhr.getJson('//example.com/api/v1', callback, error); rgjs.xhr.getJson('//example.com/api/v2', callback, error); rgjs.xhr.abortAll(); -
<static> clearCache( [url])
-
Clear internal cache.
Parameters:
Name Type Argument Description urlstring <optional>
Optional. If specified, only clears cache for the specific url.
Examples
// Clear all cache rgjs.xhr.clearCache();
// Clear cache only for a specific url rgjs.xhr.clearCache('//example.com/api/v1'); -
<static> get(url [, cb] [, cberr] [, opts])
-
XMLHttpRequest get request with sugar on top.
Parameters:
Name Type Argument Description urlstring The url for the request.
cbfunction <optional>
The callback. Arguments:
response,status,statusText,xhr.cberrfunction <optional>
The error callback. Arguments:
status,statusText,xhr.optsobject <optional>
Options are optional. See _options.
Returns:
An identifier that can be used to abort the request.
- Type
- string
Example
// Get json response and pass it to onSuccess(). const request_id = rgjs.xhr.get('//example.com/api/', onSuccess, onError, {mimeType: 'application/json'}); -
<static> getJson(url [, cb] [, cberr] [, opts])
-
Convenient
get()withopts.mimeTypeset toapplication/json.Parameters:
Name Type Argument Description urlstring The url for the request.
cbfunction <optional>
The callback. Arguments:
response,status,statusText,xhr.cberrfunction <optional>
The error callback. Arguments:
status,statusText,xhr.optsobject <optional>
Options are optional. See _options.
Returns:
An identifier that can be used to abort the request.
- Type
- string
Example
// Get json response and pass it to callback(). const request_id = rgjs.xhr.getJson('//example.com/api/', onSuccess, onError); -
<static> post(url, data [, cb] [, cberr] [, opts])
-
XMLHttpRequest get request with sugar on top.
Parameters:
Name Type Argument Description urlstring The url for the request.
dataobject An object containing key-value pairs.
cbfunction <optional>
The callback. Arguments:
response,status,statusText,xhr.cberrfunction <optional>
The error callback. Arguments:
status,statusText,xhr.optsobject <optional>
Options are optional. See See _options.
Returns:
An identifier that can be used to cancel the request.
- Type
- string
Example
// Post json data. const data = {uid: '...', account: '...'}; const request_id = rgjs.xhr.post('//example.com/api/', data, callback, error); -
<inner> _send(url [, cb] [, cberr] [, opts])
-
Send an XMLHTTPRequest.
Parameters:
Name Type Argument Description urlstring The url for the request.
cbfunction <optional>
The callback. Arguments:
response,status,statusText,xhr.cberrfunction <optional>
The error callback. Arguments:
status,statusText,xhr.optsobject <optional>
Options are optional. See _options.
Properties
Name Type Argument Description methodstring <optional>
Optional method (GET, POST, etc). Defaults to 'GET'.
bodyobject <optional>
Optional body for POST request.
Returns:
An identifier that can be used to abort the request.
- Type
- string