In the version 1.8, DOJO toolkit announced a new API dojo/request. This API simplifies the AJAX call to the server. As most of the internet applications are using the multiple AJAX request for displaying the data without re-loading the entire page, it becomes necessary for any UI framework has to support the easy and most powerful AJAX mechanism to handle the client requests. DOJO is just does that with an awesome API which can easily written by any novice programmer.
The beauty of the API is that it looks very simple, but complex things are under the curtain. Which makes the developers don’t feel the pain of writing the complex logic them self, instead DOJO takes care of making the asynchronous calls to the server. This post explains the very very simple example for understanding the new API. Which does nothing more than just making call to the server. It is good for first time programmer to read this article, for advanced users can skip this article.You may read some good dojo tutorials published in JavaBeat.
If you are new to DOJO and looking for the getting started tutorials, please refer dojo tutorials. I assume that reader has the basic knowledge on DOJO and AJAX concepts. Lets jump into the example for running this tutorial. It has one jsp with dojo code for making the call to server and another text file with simple text to be printed on alert message.
DOJO AJAX Call Example Listing 1 – dojoajax.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script> var dojoConfig = { baseUrl : "src", packages : [ { name : "dojo", location : "dojo" }, { name : "dijit", location : "dijit" }, { name : "dojox", location : "dojox" }, { name : "app", location : "app" }, ], parseOnLoad : true, async : true }; </script> <script src="src/dojo/dojo.js"></script> <script> require(["dojo/request"],function (request){ request("hello.txt").then( function (text){ console.log("File Content : " + text); alert(text); }, function (error){ console.log("Error Occurred: " + error); } ); }); </script> </head> <body> </body> </html>
DOJO AJAX Call Example Listing 2 – hello.txt
It is a test message!!
dojo/request is the new API from DOJO 1.8 to make the AJAX server calls. The above example just shows how to access a text file from the server using dojo/request. As it is very simple, it takes the first parameter as the URL (“hello.txt”) which is in the server and omits the optional parameters. First parameter in dojo/request is mandatory and other parameters are optional. We will learn about the other parameters in the next tutorial. If the server returns the value, first function will be called, the second function will be called if there is any error. If you have the DOJO SetUp in your system, you can copy these codes and run the example right away!!. If you find any issues, please write it in the comments section.