Alex Roytman: Hello and welcome back to Profound Logic TV. I’m Alex Roytman. Today, we’re going to address an inquiry from one of our viewers who asks, “Where can I find the specs for the AJAX function used in our earlier video on how to send AJAX request to the IBMI server?” Well, to help me out with this, I’ve ask David Russo, a developer who has worked extensively on building out cross browser JAVA script AJAX library, to demonstrate some of the various parameters and options of the AJAX function. So, enjoy this demonstration and at the end of the video, I will tell you where you can find detailed documentation for the AJAX library and how you can download it for free for using your web development and a serious modernization project.
David Russo: Hi, everyone and thanks for joining us today. Well, using the AJAX library typically involves creating a request object setting properties on that request object such as the URL, the HTTP request method, a synchronous mode and so on then setting events such as what happens when the request exceeds or if it fails due to an HTTP and finally initiating a method is actually send a request.
Most developers are going to use shortcut functions instead. They perform all of these steps in one JAVA script statement. There are several of this shortcut functions. The main ones are AJAX which automatically sends a request and receives a response back in row text format. A JAX XML which sends a request to expecting XML’s response and then parses the response and returns back at JAVA script XML document object and AJAX JSON which sends a request to expecting data in the JAVA script object notation.
These functions expect one or more parameters to use them synchronously; you pass only one parameter typically the URL and some query string parameters. In this example of synchronous request that’s made in a query string parameter called PARM1 that’s sent and has a value contained in variable PARM1. When we say that this is a synchronized request, we mean that the script is actually going to wait until the server response to their request before proceeding. When doing a synchronous request using one of these shortcut functions in the AJAX library, the server responses return from the function in this case into the variable response.
However, synchronous mode is generally not preferred in AJAX development because the browser has to wait for their response to come back before anything else can happen. This can sometimes cause the UI to become unresponsive during those long running request. For this reason, you generally use an A-synchronous request. When the requester made it synchronously, the script does not wait for the request to finish. The request is actually run in separate threads somewhere to a concept of submitting jobs to batch on a server. This means that the UI will still be responsive and the user can still take other actions while that request is in progress.
To use the AJAX function synchronously, you must pass the second parameter. This parameter will specify a JAVA script function that will handle the response when it does come back from the server. The handler function will be called when the request complete successfully and the response will be passed to it for processing. It’s also possible to use what is called an Anonymous function in Java script. In other words, we embed it right into the AJAX statement instead of naming the function.
Now, let’s have a look at some other parameters we can pass to the AJAX function such as the HTTP method to use or some post parameters. To pass any other parameters, you must pass them all as one entity or object. This entity replaces the URL parameter. The object notation allows you to name each parameter and give it a value. And the example you see on the screen, we’re using AJAX with the post method and we’re sending post parameters named PARM1 and PARM2.
In this case, I broke in the statement into multiple line to make it a bit easier to read and to work with. Here’s another example where we make use of another short hand function AJAX JSON. This function works identically to the AJAX function we’ve been using except it’s going to automatically evaluate the server response as a AVA script object. If this is successful, the resulting object will be passed into the handler function.
Another function that’s very similar is AJAX XML. Again, this works in exactly the same way as the other functions we’ve seen but this time, the response will be evaluated as an XML document object before passing into the handler function. Well, that about wraps up the demonstration for today. I hope that you found it helpful. I’ll turn it back to you Alex.
Alex Roytman: Thanks, David. Well, you’ve gotten a glimpse of the capabilities of some of the functions contained in the AJAX library. To learn more, simply click the resource’s tab at the top of our site www.profoundlogic.tv. There you can download the library itself and the PDF documentation associated with it. Please remember to keep those comments and questions coming. I’ll talk to you again soon.
Transcription by:
Scribe4you Transcription Services