Ajax API

As shown in the widget API document there are four major methods that can be used for preforming Ajax calls. These methods are:

Ajax Methods

Method Name Description
UDW.proxy.getText(params)Returns result as plain text
UDW.proxy.getFeed(params)Returns feed object or error object
UDW.proxy.getXML(params)Returns result as XML document
UDW.proxy.getJSON(params)Returns result as JSON object

Every method requires exact one parameter that is specified as JavaScript object. See example of using parameter:

UDW.proxy.getFeed({
    uri: 'http://rss.cnn.com/rss/edition_world.rss',
    callback: function(response) {
        //process response
    }
});

As far as Ajax calls are asynchronous it is impossible to handle response returned by invoked method immediately, thus it is required to pass callback function to the Ajax method. After request completes passed callback function is invoked with only one argument.

Type of the argument passed to the callback method depends on the invoked method. As shown above there are four methods for each particular request type.

UDW.proxy.getText

As we can see this method returns plain text after request completion. With this method it is possible to retrieve any text from any specified URI.

UDW.proxy.getText({
    uri: 'http://domain.com/document.txt',
    callback: function(response) {
        if (response) {
            widget.setContent(response);
        }
    }
});

UDW.proxy.getFeed

This method is special case. Invoking this method will return JSON object that is standardized form of feeds. Currently only RSS, Media RSS and Atom feeds of any versions are supported. Server side feed processor analyzes requested feed and builds standardized JSON object that is passed to the callback function later.

This is really useful part of the API. Instead of dealing with different types of XML documents widget authors can work with standardized JSON object. Among other advantages it is worth to note that working with JSON is significantly faster rather then parsing XML documents with DOM methods.

UDW.proxy.getText({
    uri: 'http://domain.com/rssfeed.xml',
    callback: function(feed) {
        if (feed) {
            var body = [];
            body.push('Feed title: ' + feed.title);
            body.push('Number of items: ' + feed.items.length);
            widget.setContent(body.join('<br />');
        }
    }
});

UDW.proxy.getXML

This method return XML object. This is very powerful method for working with any type of XML document.

UDW.proxy.getText({
    uri: 'http://domain.com/rssfeed.xml',
    callback: function(response) {
        if (response) {
            var doc = response.documentElement;
            var body = [];
            body.push('Feed title: ' + doc.getElementsByTagName('title')[0].nodeValue);
            body.push('Number of items: ' + doc.getElementsByTagName('item').lenght);
            widget.setContent(body.join('<br />');
        }
    }
});

UDW.proxy.getJSON

This method is intended to only work with JSON responses. Method receives and parses JSON text and passes prepared object to the callback function.

Suppose that we need to handle JSON document with following structure:

{
    "title": "This is document title",
    "description": "This is document description"
}
UDW.proxy.getText({
    uri: 'http://domain.com/jsondocument.php',
    callback: function(response) {
        if (response) {
            var body = [];
            body.push('Title: ' + response.title);
            body.push('Description: ' + response.description);
            widget.setContent(body.join('<br />');
        }
    }
});