Fetching data and interface fragments

fetch.js requests JSON data or HTML fragments for the interface.

fetch.fetch_data(url)

Fetch and decode JSON using the browser's current credentials.

HTTP, network, and JSON-decoding errors are converted to false.

Arguments:
  • url (string|URL) -- Resource URL.

Returns:

Promise.<(*|false)> -- Decoded JSON value, or false on failure.

Example

const instruments = await fetch_data('/api/instruments');
if (instruments === false) console.error('Request failed');
fetch.fetch_html(selector, url, options={})

Fetch an HTML fragment and inject it into an element.

GET data is encoded as query parameters. Any method other than the literal string "get" sends a POST request whose body is data. Request and injection errors are converted to false.

Arguments:
  • selector (string) -- CSS selector passed to dom.inject_html().

  • url (string|URL) -- Resource URL.

  • options (object) -- Request options.

  • options.method (string) -- Request method selector.

  • options.data (any) -- Query values for GET or request body for POST.

Returns:

Promise.<boolean> -- true after successful injection; otherwise false.

Example

await fetch_html('#results', '/ui/demo/results', {
  data: {brightness: 20, snr: 10}
});