fetch
This article describes a feature or macro function that is experimental and may be subject to change.
fetch() Function
• Introduced in version 1.11.4
Fetches a URI via JavaScript. Compatibility wrapper around XMLHttpRequest().
Usage
let x = fetch(target, [options]);
x.then((response) => {
// JS code here for success return (any 2XX code)
}, (failure) => {
// JS code here for failure status (any non-2XX code)
});
Parameters
target
- The macro to call or asset to retrieve.lib://<libraryName>/macro/<macroName>
style lib-URIs work, as doasset://<asset-hash>
(as returned by the getImage() function). Macros use a locationless-URImacro:macroName@macroLocation
.options
- A JavaScriptobject
mapping optional arguments to values. Notable options are discussed at XMLHttpRequest().method
- HTTP Method to emulate. UseGET
for assets/library resources,POST
for macro calls that you want to send parameters to. (UsingGET
for macros is possible, but no parameters can be passed as there is no "query string".)body
- The body of the request to send. Can be aPromise
which returns a body. Lazily coerced to string.headers
- A JavaScript object containing request headers to include. Received in an MTscript macro via macro.requestHeaders.
Like the native fetch
API in JavaScript, you can also create a new Request()
object and pass that in to fetch(). Internally, the fetch() wrapper does this for you. It then calls XMLHttpRequest() and returns a Promise
which resolves when the XMLHttpRequest() finishes.
See XMLHttpRequest() for handling macro headers and other macro special variables.