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
method.
TABLE 3-6 Parameters for the XMLHttpRequest open method
Parameter
Description
The HTTP method being used for the request: GET, POST, etc.
Method
The URL to make the request to.
URL
A Boolean value to indicate whether the call should be made asynchronously. If true, an
event handler needs to be set for the onreadystatechanged .
async
A user name if the destination requires credentials.
User name
A password if the destination requires credentials.
Password
EXAM TIP
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
server.
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();
xReq.open("GET", "myXMLData.xml", false);
xReq.timeout = 2000;
xReq.ontimeout = function () {
$("#results").text("Request Timed out");
}
xReq.send(null);
$("#results").text(xReq.response);
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