scheduler ajax module
// assuming that the response is the following
{status: "ok", data: "value", data2: "value2"}
var xhr = scheduler.ajax;
// HTTP GET
xhr.get("server.php").then(function(response) {
var res = JSON.parse(response.responseText);
if (res && res.status == "ok") {
// response is ok
}
});
// HTTP POST
xhr.post({
url:"server.php",
data: {
paramName: "paramValue"
}
}).then(function(response){
var res = JSON.parse(response.responseText);
if (res && res.status == "ok") {
// response is ok
}
});
All methods can take as parameters either:
1) RequestConfig - an object with request config options, which looks as follows:
{
url: string,
method: "PUT|GET|POST|DELETE",
data: string | object,
async: true|false
callback: function,
headers: object
}
where:
or:
2) Three parameters (except for the query() method which can take only the RequestConfig object):
The list of the ajax module API is given below:
All methods allow both callbacks or promises for response handling.
An ajax promise returns a completed XmlHttpRequest:
scheduler.ajax.post({
url:"some.php",
data: {
paramName: "paramValue"
}
}).then(function(response){
alert(response.responseText);
});
For historical reasons, the callback option receives value in a slightly different format:
scheduler.ajax.post({
url:"some.php",
data: {
paramName: "paramValue"
},
callback: function(result){
var response = result.xmlDoc;
alert(response.responseText);
}
});
the common method of sending requests. Allows sending any type of request (you need just to specify the desired request in the parameters)
scheduler.ajax.query({
url:"some.php",
method:"POST",
data: {
paramName: "paramValue"
}
}).then(function(response){
alert(response.responseText);
});
sends a GET request
scheduler.ajax.get("some.php", function(){
// your code here
});
// or
scheduler.ajax.get({
url: "https://…",
callback: function() {…},
headers: { "Content-Type": "application/json" }
});
sends a PUT request
scheduler.ajax.put("server.php", "keep_alive=1&version=std", function(){
// your code here
});
// or
scheduler.ajax.put({
url: "https://…",
callback: function() {…},
headers: { "Content-Type": "application/json" }
data: {}
});
sends a DELETE request
scheduler.ajax.del("server.php", function(){
// your code here
});
// or
scheduler.ajax.del({
url: "https://…",
callback: function() {…},
headers: { "Content-Type": "application/json" }
});
sends a POST request
scheduler.ajax.post("server.php", "keep_alive=1&version=std", function(){
// your code here
});
// or
scheduler.ajax.post({
url: "https://…",
callback: function() {…},
headers: { "Content-Type": "application/json" }
data: {}
});
You can pass an object with data instead of string into the post and put methods. In case an object is passed, the ajax module serializes it by itself. A simple object will be serialized as form data (¶m=value), nested structures will be serialized with the help of JSON.stringify().
For example, the following object:
{
id: 1,
text: "My Task",
users: [1,2,3]
}
will be converted into a string that looks like id=1&text=My%20Task&users=%5B1%2C2%2C3%5D
.
dhtmlxScheduler supports usage of promises (including IE8+). For work with promises Scheduler uses the Bluebird promise library. To create a promise, you need to use the following constructor:
var promise = new scheduler.Promise(function(resolve, reject) {...});
Promise is declared inside Scheduler, not globally for the application.
The AJAX module returns a promise, which allows using the promise interface instead of the callback. Thus instead of using
scheduler.ajax.post(url, params, callback);
For example, when sending a POST request, you can use the following record:
scheduler.ajax.post(url, params).then(function(){…});
It is possible to use callbacks and promises at the same time.
The example below shows how you can send several requests to the server at once, and reload data after that:
scheduler.Promise.all([
scheduler.ajax.post({url: "api/event", data: event1}),
scheduler.ajax.post({url: "api/event", data: event2}),
scheduler.ajax.post({url: "api/event", data: event3})
]).then(function(){
scheduler.clearAll();
scheduler.load("/api");
});
added in version 6.0
Back to top