What is AJAX?
AJAX = Asynchronous JavaScript and XML
In this tutorial, we will make an application for asynchronous calling.
The simplest way for asynchronous calling in Sigma Visual is to use function linb.request. Its definition can be found at
http://www.sigmawidgets.com/products/sigma_visual/API/index.html#linb.request.
It's declared like the following line.
It's a little complex because it takes 6 parameters. Don't worry about that. For simple code, the first three parameters are enough.
- uri: The URL of the request target. For example, "http://www.google.com/".
- query: This is web request parameters pairs. For example, "{para1:1,para2:2}".That will get the same result as we input http://www.google.com/?para1=1¶2=2 in the browser.
- onSuccess: A function to be executed whenever a request is done successfully. I will explain this function later. But now let's make an application first.
[Req Spec]1. A dialog window and a button are needed.
2. When end user clicks button, content of another html file will be retrieved and put into dialog window.
[Do It Yourself]1. Drag a [Dialog] to design area,set its alias to "dialog1"(Click dialog window to select it, type "dialog1" in [Component Config Window]->[alias],and strike [Enter] on keyboard. )
2. Drag a [Button] to design area.
3. Prepare a file named hello.html to
http://localhost/hello.html. It reads
4. Add event handler for button being clicking. (Click button to select it,click on right side of [Component Config Window]->[events]->[onClick],a small button will appear. Click this button, a dialog box will pop up.)
5. You will see the following lines in the dialog
6. In the handler, we will call linb.request. Let's have a look of code snippet.
The first parameter, "
http://localhost/hello.html", is what we will get synchronously.
The second parameter, the url parameters pairs, is set to null because hello.html is a static html file, not a php file or something else.
The third parameter is function, which will change inner html of dialog1 by calling setHtml(). Variable rep is the result of asynchronous calling to hello.html.
7. Something interesting is that we store variable this to a variable, me. It is a must because variable this in Javascript is quite different from the one in c++/java. You could get more knowledge by search "this in javascript" in google.The whole code looks like
8. Click [OK] to close dialog.
9. Press [run] to see the result.