dhtmlxAjax Guide and Samples

Main features

  • GET/POST requests
  • Multibrowser support
  • Easy incorporation in DHTMLX based applications
  • Automatic new loader creations
  • XPath
  • XSLT
  • Supported browsers

  • IE
  • Mac OS X Safari/Conqueror
  • Mozilla/Mozilla Firefox
  • Opera
  • Working with dhtmlxAjax

    Sending Requests with dhtmlxAjax

    dhtmlxAjax is static object, which means you do not need to instantiate it each time you need to send AJAX request to server. Also, if you use any dhtmlx component on your page, then you do not need to add any script file to use dhtmlxAjax - it is included in all dhtmlx components. Otherwise you need to add dhtmlxcommon.js file.

    Sending GET AJAX Request

    To send asynchronous GET request and be able to process a response you need to do the following:

    dhtmlxAjax.get(url,callbackFunction);

    where callbackFunction is a function object which will be called when response from server comes. It gets one argument called loader. This object contains all necessary information about response and some additional capabilities. In details:

  • loader.xmlDoc - HTTP Response object
  • loader.xmlDoc.responseXML - xml object which came with reponse
  • loader.xmlDoc.responseText - text which came with response
  • loader.doXPath(xpathExp, docObj, namespace, result_type) - XPath Processor (see details in related chapter)
  • loader.doSerialization() - returns string representation of XML object got with response
  • loader.doXSLTransToObject(xslDocument) - makes XSL transformation with object result (see details in related chapter)
  • loader.doXSLTransToString(xslDocument) - makes XSL transformation with string result (see details in related chapter)
  • To send synchronous GET request you need to do the following:

    var loader = dhtmlxAjax.getSync(url);

    In case of synchronous request loader object will be returned as a result. It has all features described above.

    Sending POST AJAX Request

    The only difference in making POST request with dhtmlxAjax is additional argument next to URL - POST parameters:

    dhtmlxAjax.post(url,params,callbackFunction); var loader = dhtmlxAjax.postSync(url,params);

    where params is a string of name=value pairs united with &, like "param1=somevalue&param2=someothervalue"

    XPath with dhtmlxAjax

    XPath is a language for addressing parts of an XML document. Complete W3C Specification can be found here. dhtmlxAjax supports XPath for loader object (it comes to the callback function as an incomming argument in case of async request or you get it as a result of getSync and postSync methods). Common way of usage is:

    var loader = dhtmlxAjax.getSync(url); loader.doXPath(xpathExp, docObj, namespace, result_type);

    where:

  • xpathExp - XPath expression, like "/itemsitem[@id='123']"
  • docObj - XML document object. In case it is null XPath expression will be procesed against XML document which came in response.
  • namespace - namespace to take into account if any. Use null if no namespace required
  • result_type - by default result will be an array of found elements, but if you set last parameter to "single", the first found element will be returned
  • Thus in most simple cases the following syntax like this is enough. It will return array of found elememts. You can itterate through them using common way of itterating through array.

    var result = loader.doXPath("/some/expression[@arg='value']"); for(var i=0;i<result.length;i++){ alert(result[i].nodeName) }

    XSLT with dhtmlxAjax

    XSLT - language for transforming XML documents into other XML documents (commonly used for creating HTML from XML). Complete specification of XSLT can be found here. dhtmlxAjax provides two methods for making XSL Transformation:

  • loader.doXSLTransToObject(xslDocument) - makes XSL transformation of XML document which came in request with object as a result
  • loader.doXSLTransToString(xslDocument) - makes XSL transformation with string representation of a result
  • In both cases incoming argument is xslDocument object. It can be loaded with dhtmlxAjax, as xsl is also xml document. Possible variant of usage:
    dhtmlxAjax.get("xsl_file.xsl",function(xslLoader){ var xmlLoader = dhtmlxAjax.getSync("xml_file.xml"); some.innerHTML = xmlLoader.doXSLTransToString(xslLoader.xmlDoc.responseXML); })