Tuesday, September 7, 2010

AJAX - Request a Server


AJAX - Request a Server

AJAX - Sending a Request to the Server
To send off a request to the server, we use the open() method and the send() method.
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
The open() method takes three arguments
•        The first argument defines which method to use when sending the request (GET or POST).
•        The second argument specifies the URL of the server-side script.
•        The third argument specifies that the request should be handled asynchronously.
The send() method sends the request off to the server.

AJAX - Sending a Request to the Server

The send() method sends the request off to the server
When the statement req.send(null); is reached, the call will be made. In the case of
an HTTP GET, this content may be null or left blank. When this function is called on
the XMLHttpRequest object, the call to the URL that was set during the configuration
of the object is called.
When using the HTTP GET method, the length of URL, including escaped URL
parameters, is limited by some browsers and by server-side web containers. The
HTTP POST method should be used when sending data to the server that will affect
the server-side application state.

An HTTP POST requires a Content-Type header to be set on the XMLHttpRequest
object by using the following statement:
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("id=" + encodeURIComponent(idTextField.value));
JavaScript technology includes an encodeURIComponent() function that should be
used to ensure that localized content is encoded properly and that special characters
are encoded correctly to be passed in an HTTP request.

Following items represent the setup of an Ajax interaction:
•         A client event occurs.
•         An XMLHttpRequest object is created and configured.
•        The XMLHttpRequest object makes a call.
•        The request is processed by the server side code.
•         The server side code returns an XML document containing the result.
•        The XMLHttpRequest object calls the callback() function and processes the result.
•        The HTML DOM is updated.


No comments:

Post a Comment