Jump to content

Asynchronous HTTP requests in jSignage

From SpinetiX Wiki

This page is related to jSignage API.

Overview

AJAX allows a jSignage document to exchange data with a remote server asynchronously, without reloading or restarting the document. It is commonly used to retrieve data feeds, query web services, and send data to HTTP endpoints.

jSignage provides a subset of the jQuery AJAX API, adapted to the SVG Tiny 1.2 µDOM environment. It also provides additional methods for retrieving and processing data feeds.

Only the HTTP GET and POST methods are supported. The response can typically be processed as text, JSON, or XML, depending on the method and the specified data type.

jQuery AJAX methods

The following jQuery Ajax methods are implemented in jSignage:

  • $.ajax( url, [settings] )
    Low-level interface providing full control over the HTTP request.. Note that only the GET and POST HTTP methods are supported by the SVG Tiny 1.2 µDOM.
  • $.get( url, [data], success( data,textStatus ), [dataType] )
    Sends an HTTP GET request.
  • $.getJSON( url,[data], success( data, textStatus ) )
    Sends an HTTP GET request and processes a JSON response.
  • $.post( url, [data], success( data, textStatus ), [dataType] )
    Sends an HTTP POST request.
Note Note:
Learn about Ajax or see jQuery documentation for reference about these methods.
Note Warning:
When using AJAX methods in browsers, the security settings may prevent data from being loaded if it does not comes from the same server as the file being displayed. There are no issues on the HMP devices or Elementi, but for cross-browser / player compatibility, JSONP extension can be used to overcome the cross-domain restrictions imposed by browsers' same-origin policy.

jSignage data-request utilities

  • $.getAndParseFeed( dataSourceObject, callback, useGetURL )
    Retrieves and parses a data feed according to a jSignage data-source configuration.
  • $.setGetURLFlags( flags )
    Sets the global caching flags used by subsequent HTTP GET requests. The flags are detailed under the getURL() function, a SpinetiX addition to the SVG Micro DOM (uDOM).

Examples

Request an RSS feed

Request an RSS feed and parse it using parseRSS:

$.get(
   'http://rss.news.yahoo.com/rss/world',
   function( data ) {
     var news = $.parseRSS( data );
     ...
   },
   'text'
 );

Notice that you must use set the dataType parameter to 'text' in order to prevent the feed from being parsed to XML as a result of autodetection.

Query a database

Query a database via a PHP script located on the server and returning the data as JSON:

$.getJSON(
   'http://Path_to_File_Location/db.php',
   { table: 'prices', type: 'coffee', subtype: 'latte' },
   function( response ) {
     $('#price').clearText().tspan( response.price );
   }
 }
Update a Shared Variable via RPC

To set / update a Shared Variable via RPC, an AJAX POST call to http://HMP_address/rpc/can be used:

$.ajax( { 
  type: "POST", url: "http://HMP_address/rpc", contentType: 'application/json',
  data: JSON.stringify( {
    method: "webstorage_set", params: [[ { name: "var1", value: "Hello" } ]], id: 1
  } )
});

See also

We use only essential cookies for site functionality.