HTML and CSS Reference
In-Depth Information
This script assumes a button on the HTML form and a div to show the results. A new
XMLHttpRequest object is created. The open method is called to specify the request type, URI,
and whether to make the call asynchronous. Table 3-6 lists all the parameters to the open
TABLE 3-6 Parameters for the XMLHttpRequest open method
The HTTP method being used for the request: GET, POST, etc.
The URL to make the request to.
A Boolean value to indicate whether the call should be made asynchronously. If true, an
event handler needs to be set for the onreadystatechanged .
A user name if the destination requires credentials.
User name
A password if the destination requires credentials.
The open method doesn't make any server requests. If the user name and password is
specified, it doesn't send this information to the server in the open method. When the
send method is called, the user name and password aren't passed to the server either. The
credentials are passed to the server only in response to a 401 security response from the
The XMLHttpRequest object provides some mechanisms for handling errors. The most
common error to account for is a timeout error. By default, the value of the timeout is zero,
which is infinite. A timeout value should always be specified. The code is updated as follows:
var xReq = new XMLHttpRequest();"GET", "myXMLData.xml", false);
xReq.timeout = 2000;
xReq.ontimeout = function () {
$("#results").text("Request Timed out");
This results in not allowing the call to take any more than two seconds. The timeout is
expressed in milliseconds. After the timeout period, the ontimeout event handler is called to
allow for this condition to be handled appropriately in the webpage.
An additional consideration for this code is whether to make the call synchronously or
asynchronously. Ideally, you should ensure that the call to the service to get the data won't
interfere with users and won't block them, unless of course they need to wait on the reply
before taking any further action. Synchronous calls, as the examples so far have shown,
Search WWH ::

Custom Search