fetch: Difference between revisions
Jump to navigation
Jump to search
m (Fixed top-level macro formatting; reformatted JS example) |
|||
Line 7: | Line 7: | ||
|usage= | |usage= | ||
<syntaxhighlight lang="javascript" line> | <syntaxhighlight lang="javascript" line> | ||
let x = fetch(target, [options]) | let x = fetch(target, [options]); | ||
x.then((response)=> { | x.then((response) => { | ||
}, | }, (failure) => { | ||
}); | |||
} | |||
); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 21: | Line 19: | ||
{{param|body|The body of the request to send. Can be a promise which returns a body. Lazily coerced to string}} | {{param|body|The body of the request to send. Can be a promise which returns a body. Lazily coerced to string}} | ||
{{param|headers|A javascript ojbect containing request headers to include}} | {{param|headers|A javascript ojbect containing request headers to include}} | ||
Like the native <code>fetch</code> API in javascript, you can also create a <code>new Request()</code> object and pass that in to {{func|fetch}}. Internally, the {{func|fetch}} wrapper does this for you. It then calls {{func|XMLHttpRequest}} and returns a promise which resolves when the {{func|XMLHttpRequest}} finishes. | Like the native <code>fetch</code> API in javascript, you can also create a <code>new Request()</code> object and pass that in to {{func|fetch}}. Internally, the {{func|fetch}} wrapper does this for you. It then calls {{func|XMLHttpRequest}} and returns a promise which resolves when the {{func|XMLHttpRequest}} finishes. | ||
See {{func|XMLHttpRequest}} for handling macro headers and macro special variables. | See {{func|XMLHttpRequest}} for handling macro headers and macro special variables. | ||
<h3>See Also</h3> | <h3>See Also</h3> | ||
Line 37: | Line 31: | ||
[[Category:HTML5 javascript]] | [[Category:HTML5 javascript]] | ||
}} |
Revision as of 05:01, 2 June 2023
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. Wrapper around XMLHttpRequest().
Usage
let x = fetch(target, [options]);
x.then((response) => {
}, (failure) => {
});
Parameter
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 belowmethod
- HTTP Method to emulate. GET for assets/library resources, POST for macro calls.body
- The body of the request to send. Can be a promise which returns a body. Lazily coerced to stringheaders
- A javascript ojbect containing request headers to include
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 macro special variables.