Lecture 9 Ajax
Lecture 9 Ajax
● Asynchronous: user can keep interacting with the page while data loads
The XMLHttpRequest object
● Javascript has an XMLHttpRequest object that can fetch files from a web server
o supported in IE11+, Safari, Firefox, Opera, Chrome, etc.
o IE5/6 don’t have it, but it’s 2021
o Microsoft invented XMLHttpRequest and Ajax for use in an online version of
Outlook in 1999 (credit where it’s due!)
● it can do this asynchronously (in the background, transparent to the user)
● the contents of the fetched file can be put into current web page using the DOM
A typical Ajax request
1. user clicks, invoking an event handler
2. handler’s code creates an XMLHttpRequest object
3. XMLHttpRequest object requests page from server
4. server retrieves appropriate data, sends it back
5. XMLHttpRequest fires an event when data arrives
o this is often called a callback
o you can attach a handler function to this event
6. your callback event handler processes the data and displays it
XMLHttpRequest methods
the core Javascript object that makes Ajax possible
Method Description
open(method, url, async) specifies the URL and HTTP request method
send() sends the HTTP request to the server, with optional POST parameters
send(postData)
responseXML the entire contents of the fetched page, as an XML document tree (seen later)
statusText HTTP status code text (e.g. “Bad Request” for 400)
timeout how many MS to wait before giving up and aborting the request (default 0 = wait forever)
readyState request’s current state (0 = uninitialized, 1 = set up, 2 = sent, 3 = in progress, 4 = complete)
Synchronized requests (bad)
// this code is in some control's event handler
var ajax = new XMLHttpRequest();
ajax.open("GET", url, false);
ajax.send();
do something with ajax.responseText; JS
● better solution: use an asynchronous request that notifies you when it is complete
o this is accomplished by learning about the event properties of XMLHttpRequest
XMLHttpRequest events
Event Description
● web servers return status codes for requests (200 means Success)
● you may wish to display a message or take action on a failed request
Handling the error event
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.onerror = errorFunctionName;
ajax.open("GET", url, true);
ajax.send();
…
function errorFunctionName(e) {
do something with e, this.status, this.statusText, ...
} JS
● the graceful way to handle errors is to listen for the error event
● the handler is passed the error/exception as a parameter
● you can examine the error, as well as the request status, to determine what
went wrong
Example Ajax error handler
var ajax = new XMLHttpRequest();
…
ajax.onerror = ajaxFailure;
…
function ajaxFailure(exception) {
alert("Error making Ajax request:" +
"\n\nServer status:\n" + this.status + " " +
this.statusText + "\n\nServer response text:\n" +
this.responseText);
if (exception) {
throw exception;
}
JS
}
● for user’s (and developer’s) benefit, show an error message if a request fails
Passing query parameters to a request
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.open("GET", "url?name1=value1&name2=value2&...", true);
ajax.send(); JS