{"id":2671,"date":"2015-01-12T19:45:47","date_gmt":"2015-01-12T15:45:47","guid":{"rendered":"http:\/\/www.dhtmlx.com\/blog\/?p=2671"},"modified":"2023-05-05T13:50:29","modified_gmt":"2023-05-05T10:50:29","slug":"how-to-work-with-ajax","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/how-to-work-with-ajax\/","title":{"rendered":"DHTMLX Little-Known Features: How to Work with Ajax"},"content":{"rendered":"<p>Based on frequent questions about working with DHTMLX and Ajax, we decided to explain how it works in detail.<\/p>\n<p>The main function of Ajax is interaction with a server, sending requests and processing of user data in particular. dhtmlxAjax was simplified in the version 4.0.<\/p>\n<p>The general scripts of working with Ajax are the following:<\/p>\n<ul>\n<li>data request from the server<\/li>\n<li>processing of the server response<\/li>\n<li>error handling<\/li>\n<\/ul>\n<p>\nWe&#8217;ll use PHP as a server script, but in general any server script can generate the content.<\/p>\n<p>For data request either <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_get.html\" target=\"_blank\">GET<\/a> or <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_post.html\" target=\"_blank\">POST<\/a> requests are used. With GET we have the following:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"kw1\">get<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;server.php?p1=v1&amp;p2=v2&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span>...<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The first param is the address of the server script server.php, there also can be any number of additional parameters p1=v1, p2=v2 (but don&#8217;t forget about the limit of GET request). As GET is an asynchronous request, we need the second param &#8211; a function that is called after the response is loaded (we&#8217;ll talk about it later). <\/p>\n<p>POST request is similar to GET, but here we pass the parameters as a separate argument:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"me1\">post<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;server.php&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;p1=v1&amp;p2=v2&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span>...<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The examples of work with synchronous requests <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_getsync.html\" target=\"_blank\">getSync<\/a> and <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_postsync.html\" target=\"_blank\">postSync<\/a> you can find in our docs.<\/p>\n<p>So, the data are requested by client. Now let&#8217;s see how to process them on the server and create a response for the client side. The most secure way is to pass data by means of using some structure, e.g. json or xml. Why them? These formats are described and they are the same in any environment. We won\u2019t show the business logic operations in our example, just the received result.<br \/>\n<!--more--><br \/>\nWe&#8217;ll start with json. Let&#8217;s suppose that the server returns the following:<\/p>\n<div class=\"codecolorer-container php default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"php codecolorer\"><span class=\"kw2\">&lt;?php<\/span><br \/>\n<span class=\"kw1\">echo<\/span> <span class=\"st_h\">'{status: &quot;ok&quot;, code: 16, error: &quot;none&quot;, id: 658}'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"sy1\">?&gt;<\/span><\/div><\/div>\n<p>Now let&#8217;s get back to the client and process the response got from the server:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"kw1\">get<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;server.php&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>r<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ use built-in function to convert string to json<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">var<\/span> t <span class=\"sy0\">=<\/span> dhx4.<span class=\"me1\">s2j<\/span><span class=\"br0\">&#40;<\/span>r.<span class=\"me1\">xmlDoc<\/span>.<span class=\"me1\">responseText<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>t <span class=\"sy0\">!=<\/span> <span class=\"kw2\">null<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\">\/\/ response successfuly parsed<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\">\/\/ response is not in json format<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>When dealing with xml, don&#8217;t forget to make sure that the client got the correct content-type header, it should be \u201ctext\/xml\u201d:<\/p>\n<div class=\"codecolorer-container php default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"php codecolorer\"><span class=\"kw2\">&lt;?php<\/span><br \/>\n<span class=\"kw3\">header<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Content-Type: text\/xml&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">echo<\/span> <span class=\"st_h\">'&lt;?xml version=&quot;1.0&quot;?&gt;'<\/span><span class=\"sy0\">.<\/span><br \/>\n<span class=\"st_h\">'&lt;root status=&quot;ok&quot; code=&quot;16&quot; error=&quot;none&quot; id=&quot;658&quot;\/&gt;'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"sy1\">?&gt;<\/span><\/div><\/div>\n<p>And on the client side:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"kw1\">get<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;server.php&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span>r<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">var<\/span> xml <span class=\"sy0\">=<\/span> r.<span class=\"me1\">xmlDoc<\/span>.<span class=\"me1\">responseXML<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>xml <span class=\"sy0\">!=<\/span> <span class=\"kw2\">null<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\">\/\/ response successfuly parsed<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">var<\/span> root <span class=\"sy0\">=<\/span> xml.<span class=\"me1\">getElementsByTagName<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;root&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">var<\/span> status <span class=\"sy0\">=<\/span> root.<span class=\"me1\">getAttribute<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;status&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ response is not in json format<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<h3><strong>Error handling<\/strong><\/h3>\n<p>\nSometimes something goes wrong and we have an error during the request processing on the server. It can be connection error, expired session or just a bug in the server code. DHTMLX Ajax provides <a href=\"https:\/\/docs.dhtmlx.com\/api__refs__dhtmlxajax_events.html\" target=\"_blank\">two events<\/a> for specifying custom handlers of server errors.<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">attachEvent<\/span><span class=\"br0\">&#40;<\/span>\u201conAjaxError\u201d<span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>The Event takes place each time when the server returns the response with incorrect status (404 &#8211; Page not found, 500 &#8211; server error, and so on).<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">attachEvent<\/span><span class=\"br0\">&#40;<\/span>\u201conLoadXMLError\u201d<span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>The event occurs only when working with xml and occurs when the returned data are not valid (response can&#8217;t be parsed as xml correctly).<\/p>\n<h3><strong>REST requests<\/strong><\/h3>\n<p><\/br><br \/>\nAlthough we\u2019ve reviewed only POST and GET requests, dhtmlxAjax supports all REST operations &#8211; GET, POST, <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_put.html\" target=\"_blank\">PUT<\/a>, <a href=\"https:\/\/docs.dhtmlx.com\/api__dhtmlxajax_del.html\" target=\"_blank\">DELETE<\/a>. For two last requests we can use:<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"me1\">put<\/span><span class=\"br0\">&#40;<\/span>\u201csome.<span class=\"me1\">php<\/span>\u201d<span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>and<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"kw1\">delete<\/span><span class=\"br0\">&#40;<\/span>\u201csome.<span class=\"me1\">php<\/span>\u201d<span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>Syntax and behaviour are the same as in in case of dhx4.ajax.post.<\/p>\n<h3><strong>Complex requests<\/strong><\/h3>\n<p><\/br><br \/>\nSometimes all the described above isn&#8217;t enough. In this case we can use dhx4.ajax.query method which allows implementing all of the mentioned operations and even more.<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">dhx4.<span class=\"me1\">ajax<\/span>.<span class=\"me1\">query<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; method<span class=\"sy0\">:<\/span>\u201dPOST\u201d<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; url<span class=\"sy0\">:<\/span>\u201dsome.<span class=\"me1\">php<\/span>\u201d<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; data<span class=\"sy0\">:<\/span>\u201dk1<span class=\"sy0\">=<\/span>v1<span class=\"sy0\">&amp;<\/span>k2<span class=\"sy0\">=<\/span>v2\u201d<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; async<span class=\"sy0\">:<\/span><span class=\"kw2\">true<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; callback<span class=\"sy0\">:<\/span><span class=\"kw1\">function<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span>...<span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; headers<span class=\"sy0\">:<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u201cMyHeader<span class=\"sy0\">-<\/span>Name1\u201d<span class=\"sy0\">:<\/span>\u201dvalue\u201d<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u201cMyHeader<span class=\"sy0\">-<\/span>Name2\u201d<span class=\"sy0\">:<\/span>\u201dvalue\u201d<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>As we see from the example, this method allows using all types of requests to the server, using sync and async API, pass additional data or specify a function for handling the server response.<\/p>\n<p>Also this method has &#8220;headers&#8221; param that allows specifying additional Request Headers. This API can be used in quite specific cases, for loading data from third party API and data services.<\/p>\n<p>We tried to cover all the questions about using of DHTMLX Ajax, but if you still have something to ask, feel free to leave your comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Based on frequent questions about working with DHTMLX and Ajax, we decided to explain how it works in detail. The main function of Ajax is interaction with a server, &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,71,1],"tags":[33,43,14],"class_list":["post-2671","post","type-post","status-publish","format-standard","hentry","category-posts-for-codeproject-com","category-tutorials","category-uncategorized","tag-all-posts","tag-dhtmlx-tips","tag-tips"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/comments?post=2671"}],"version-history":[{"count":1,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2671\/revisions"}],"predecessor-version":[{"id":11406,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/2671\/revisions\/11406"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=2671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=2671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=2671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}