The Ajax and Request Objects

Ajax and request objects allow you to send requests to the server and process response sent back from the server. Requests contain XML data that you want to process on the server. Responses depend completly on your application.

Example

var ddlLanguages = new TDropdownList();
var layHorizontal = new THorizontalLayout();
var wndAjaxRequestSample = createWindow('Ajax Request Sample');

var onLanguagesError = function(text) {
      TMessageBox.showModal('Error', 'Could not get languages!');
};

var onLanguagesOk = function(xmlDoc, text) {
      ddlLanguages.clear();

      var elements = xmlDoc.getElementsByTagName('Language');

      for (var i = 0; i < elements.length; i++) {
         var nodes = elements[i].childNodes;

         if (nodes.length == 2) {
         if (nodes.item(0).firstChild != null && nodes.item(0).firstChild.nodeValue != null && nodes.item(1).firstChild != null && nodes.item(1).firstChild.nodeValue != null) {
             ddlLanguages.add(nodes.item(0).firstChild.nodeValue, nodes.item(1).firstChild.nodeValue);
          }
         }
      }

      ddlLanguages.setSelectedFirst();
      wndAjaxRequestSample.refresh();
};

var request = new TRequest();
TAjax.sendRequest(wndAjaxRequestSample, 'GetLanguageList.php', request.getRequest(), onLanguagesOk, onLanguagesError);

layHorizontal.add(new TText('Languages:'));
layHorizontal.add(ddlLanguages);
wndAjaxRequestSample.add(layHorizontal);
showModal(wndAjaxRequestSample);

In the previous example, we sent a request to the server to fetch languages names and added them to the Dropdown List object.

Ajax Object

Ajax object is a shared object that you can use to send asynchronous requests to the server:

TAjax.sendRequest(parentObject, serverScript, data, okCallback, errorCallback, contentType);

Where:

Request Object

Request object will structure and hold request data as XML. Use add method to add data to the request object. Form:

add(propertyName, propertyValue, encode)

Where:

var request = new TRequest();
request.add(name, value); // <name>value</name>

Use getRequest property to get data held in the request object: request.getRequest();.