Module: siteKiosk.network.ajax

This module provides a load method to acquire content from an arbitrary URL via GET. Because of cross-site-scripting protection in the browser you need this if the domain or protocol of the URL to be loaded is different than the one your app is loaded from. The browser's own XMLHttpRequest cannot be used in this case. The functions are asynchronous (obviously) and expect some callbacks that will be called depending on the result of the request. The response will be parsed to a string, an XMLDocument, an object or an Array of nodes, depending on the function you choose.

Example

// assume the url http://foo.bar returns a json parsed string: "{foo: 'bar'}"

siteKiosk.network.ajax.get('http://foo.bar', 'json', function(content) {

    //content is now a regular javascript object since the request did not fail,
    //this will log 'bar'
    console.log(content.foo);


}, function(errorMessage) {
    //error handling goes here
});

Methods

<static> get(url, type, loadedCallback, failedCallback)

Performs an HTTP request and parses the content.
Parameters:
Name Type Description
url string The request URL
type string The expected response type ("text", "xml", "json" or "html")
loadedCallback siteKiosk.network.ajax~loadedCallback A callback, which will be called when the content is loaded
failedCallback siteKiosk.network.ajax~failedCallback A callback, which will be called when the request failed

<static> load(url, loadedCallback, failedCallback)

Performs an HTTP request without parsing.
Parameters:
Name Type Description
url string The request URL
loadedCallback siteKiosk.network.ajax~loadedCallback A callback, which will be called when the content is loaded
failedCallback siteKiosk.network.ajax~failedCallback A callback, which will be called when the request failed

Type Definitions

failedCallback(errorMessage)

An callback, being invoked, when an HTTP request failed.
Parameters:
Name Type Description
errorMessage string The error message

loadedCallback(content)

An callback, being invoked, when an HTTP response has been loaded.
Parameters:
Name Type Description
content string | XMLDocument | Object | Array.<Node> The response content, whose type depends on the expected response type ("text", "xml", "json" or "html")