Class: Ajax

(abstract) jrt.Ajax()

This class enables AJAX requests on classes that extend it. It also contains static methods to read HTML include files and JSON data files synchronously.

Constructor

(abstract) new Ajax()

Members

(static) obj

{Object}

(protected, constant, non-null) app :jrt.App

Reference to the app instance, just for convenience.

Type:

Methods

(static) include(filename, elemnon-null)

Includes a HTML file into a DOM element sychronously. The previous element's contents are deleted.

The app's version string is added to the file's URL so that a fresh version of the file (instead of a cached version) is used when the app version changes (see jrt.App#version).

Parameters:
Name Type Description
filename string

The HTML file name URL (absolute or relative to the app root).

elem HTMLElement

The element that will receive the file contents.

Throws:

If the file does not exist or cannot be loaded.

Type
Error

(static) loadJsonFile(filename, allowCommentsopt) → (non-null) {*}

Gets the contents of a JSON file synchronously. Standard JavaScript comments are allowed in the input file if 'allowComments' is true.

The app version is added to the file's URL so that a fresh version of the file (instead of a cached version) is used when the app version changes (see jrt.App#version).

Parameters:
Name Type Attributes Description
filename string

The JSON file name, relative to the app root.

allowComments boolean <optional>
Throws:

If there is an error loading or parsing the file.

Type
Error
Returns:

The file contents as a JavaScript object or value.

Type
*

(protected) processResponse(reqnon-null, status, responsenullable)

Implement this method to process the response received after a call to sendRequest. The response data type depends on the responseType property specified in the request.

The response status can be "success", "server-error", "type-error" (when the specified response type does not match the received data), "error" (if there is a connection error), "timeout" or "offline". Note that the "offline" status is not reliable in all devices, so an "error" status may be received when offline.

The default implementation does nothing.

Parameters:
Name Type Attributes Description
req jrt.AjaxRequest

The request object.

status string

The response status (see method description).

response jrt.AjaxResponse <nullable>

The server response. This will be null on error or timeout.

(protected) sendRequest(reqnon-null)

Sends an asynchronous AJAX request to the URL specified in req'. The supported methods are "GET" (the default), "POST", "PUT", "PATCH", "DELETE" and "HEAD". The response will be passed to processResponse. The expected response type can be specified with the responseType property in the specified request ("json" by default).

Parameters:
Name Type Description
req jrt.AjaxRequest
Throws:

If any of the request parameters is invalid.

Type
Error