Check documentation for the latest version of dhtmlxSuite post DHTMLX Docs

post

performs an async-POST ajax request

void post(string url, [string params,function callback] );
urlstringurl to the server side
paramsstringoptional, POST params
callbackfunctionoptional, a function to call after the response is loaded

Example

// simple data sending w/o post params
dhx.ajax.post("server.php?keep_alive=1");
 
// simple data sending with params
dhx.ajax.post("server.php", "keep_alive=1&version=std");
 
// data sending w/o params with callback
dhx.ajax.post("server.php", function(){
    // your code here
});
 
// data sending with params and callback
dhx.ajax.post("server.php", "keep_alive=1&version=std", function(){
    // your code here
});

Details
  • to prevent caching in some browsers an extra param like "dhxr0123456789" will be added
    (to disable: dhx.ajax.cache=true, please check details here)

  • async POST request will be performed

var t = 1;
dhx.ajax.post("server.php", function(){
    t = 2;
});
console.log(t); // still 1
  • getting xml from response, make sure a server returns valid xml and the "content-type" header is set
// for php
header("Content-Type: text/xml");
<!-- assuming that the response is the following -->
<items>
    <item name="first" value="a"/>
    <item name="second" value="b"/>
</items>
dhx.ajax.post("server.php", "action=get_nodes", function(r){
    var items = [];
    var xml = r.xmlDoc.responseXML;
    var nodes = xml.getElementsByTagName("items");
    for (var q=0; q<nodes.lengt; q++) {
        items.push({
            name:   nodes[q].getAttribute("name"),
            value:  nodes[q].getAttribute("value")
        });
    }
});
  • getting json from response
// assuming that the response is the following
{status: "ok", data: "value", data2: "value2"}
dhx.ajax.post("server.php", "action=get_nodes", function(r){
    var t = dhx.s2j(r.xmlDoc.responseText); // convert response to json object
    if (t != null && t.status == "ok") {
        // response is ok
    }
});
  • you can both get/post params
dhx.ajax.post("server.php?get1=1&get2=2", "post3=3&post4=4");
print_r($_REQUEST);
// result
Array
(
    [get1] => 1
    [get2] => 2
    [post3] => 3
    [post4] => 4
)
 
print_r($_GET);
// result
Array
(
    [get1] => 1
    [get2] => 2
)
 
print_r($_POST);
// result
Array
(
    [post3] => 3
    [post4] => 4
)
Change log

added in 4.0

Back to top