fetch: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Created page with "{{Experimental}} {{MacroFunction |name=fetch |version=1.11.4 |description=Fetches a URI via javascript. Wrapper around {{func|XMLHttpRequest}}. |usage= <syntaxhighlight lang...")
 
No edit summary
Line 26: Line 26:


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>
[[XMLHttpRequest|XMLHttpRequest]]<br/>
[https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API Fetch API at MDN]<br/>




[[Category:HTML5 javascript]]
[[Category:HTML5 javascript]]

Revision as of 01:34, 29 December 2021

 This article describes a feature or macro function that is experimental and may be subject to change.

{{MacroFunction |name=fetch |version=1.11.4 |description=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 do asset://<asset-hash> (as returned by the getImage() function). macros use a locationless-URI macro:macroName@macroLocation
  • options - A javascript object mapping optional arguments to values. Notable options are below
  • method - 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 string
  • headers - 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.



See Also

XMLHttpRequest
Fetch API at MDN