Pages: 1 2 3 4

Step 3 – Making the Ajax call

Now that we have the basic setup we can add AJAX. As I stated at the start of the article, Teleport will do a simple GET or POST request. In this example I’ve chosen for the REST protocol, which does a GET. I don’t have to generate some special format on the server or worst use somekind of complicated server-side framework, I simply have to make a script which output the help.

[snip php-source]code/llama-teleport/step3a.php[/snip]

Teleport is packages into 1 javascript file, which needs to be included in the head HTML page. The zip contains 2 versions: teleport_debug.js and teleport_release.js. The debug version will show an error if you make an error in the teleport definition. The release version doesn’t, but it’s smaller and quite a bit faster. For now we’ll use the debug version, but in real-life it’s advisible to make a symbolic link called teleport.js, so you can easily switch to release version when releasing your project.

[snip html-source]code/llama-teleport/step3b.html[/snip]

We need to define the RPC call using tag in the body of the HTML. The exact syntax of the definition and some more example can be found in the cheat sheet which can also be downloaded.

[snip html-source]code/llama-teleport/step3c.html[/snip]

When the result of the AJAX request is received function llama_setMessage() is called.

[snip js-source]code/llama-teleport/step3d.js[/snip]

The server might have just crashed or the Internet connection of the user might have just gone away or there might have been some other error. Therefor it is necessary to check for an error. If all is ok, we can continue with getting the messagebox using getElementById() and fill innerHTML with the result of the server and display the message box.
Last up is calling the RPC function from the button, replacing the llama_display() with llama_comm.getMessage(MSGID)

[snip html-source]code/llama-teleport/step3e.html[/snip]

Thats all there is to it.

Pages: 1 2 3 4