Pages

Welcome !!!

Hi,
Welcome to the Tech-Info world where you will be provided with the latest Tech-Updates and much more !!!

Just have a look !!!

Saturday, April 30, 2011

Handlind AJAX request with JAVA...


3. AJAX Requests

There are a lot of AJAX libraries these days. YUI's AJAX implementation has a lighter footprint than some other libraries. The Connection Manager class handles AJAX requests in YUI. It provides a simple but powerful interface while handling cross-browser instantiation of the XMLHttpRequest, handles' states, and server responses.
To use the Connection Manager, include the prerequisites Yahoo and Custom Event, and then the Connection Manager.



When you want to handle a response from an AJAX call, create a callback object to handle the server response and the returned data. If you don't care what information comes back from the server, you can omit the callback object.
var callback =
{
   success: function(o) {/*success handler code*/},
   failure: function(o) {/*failure handler code*/},
}
The response object is passed into each function in your callback object. If you need to pass in additional objects, numbers, strings, or arrays to your callback methods, you can do this by using the optional argument member.
Each of these members is optional. The Connection Manager will automatically call either success or response member functions depending on the server response. Each of these members is optional because you are overriding the callback object in the Connection Manager and passing in a custom methods for handling the response. If there is no response method defined in your code, nothing happens.
To make an AJAX request, you also need to define the data you want to send to the server, the URL to send the data to, and the method to use. In this example, you'll use the following URL, URL param string, and the HTTP POST method. If the data string has special characters, you would need to encode the postData string.
var sUrl     = 'http://myserver.com/ajax_url';
var postData = 'username=anonymous&password=blank';
var myMethod = 'POST';
At this point, you have defined methods defined to handle success and failure, your designation URL, your POST data string, and your HTTP method. Now, you just need to make your AJAX request using the Connection Manager. Use the asyncRequest method of the Connection Manager to query the server.
var request = YAHOO.util.Connect.asyncRequest(myMethod, sUrl,
   callback, postData);
Note:
You also could retrieve a form element from the page by using the DOM class and pass the form object in to the Connection Manager.

No comments:

Post a Comment