Asynchronous HTTP requests in jSignage
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
GETrequest.
- Sends an HTTP
$.getJSON( url,[data], success( data, textStatus ) )- Sends an HTTP
GETrequest and processes a JSON response.
- Sends an HTTP
$.post( url, [data], success( data, textStatus ), [dataType] )- Sends an HTTP
POSTrequest.
- Sends an HTTP
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
GETrequests. The flags are detailed under thegetURL()function, a SpinetiX addition to the SVG Micro DOM (uDOM).
- Sets the global caching flags used by subsequent HTTP
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 );
}
}
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
} )
});