AJAX - The Complete Reference

(avery) #1

190 Part II: Applied Ajax^


AjaxTCR.data.encode64() and AjaxTCR.data.decode64(), as well as to manually
address JSON, AjaxTCR.data.encodeJSON() and AjaxTCR.data.decodeJSON().
An example of form serialization with the different data formats can be found at
http://ajaxref.com/ch5/requestexplorerrevisited.html. On the handling of responses, the
library helps, but ultimately, the meaning of a response must be decoded by the user. However,
the library does provide one useful method here: AjaxTCR.data.encodeAsHTML(), which
translates any tags in a passed string to an escaped format with character entities (<
and >) as well as newlines to <br /> tags. This method will be useful to show the
tag content of the response if you are interested in it. Beyond that, there aren’t many things
to automate for response handling. However, despite this limitation, the example at
http://ajaxref.com/ch5/ responseexplorerrevisited.html certainly is much cleaner than
in Chapter 4 with the library in use.

File Upload Handling
File attachments are a particularly troubling form of data to address with Ajax. While it is
not directly possible (as of yet) to easily deal with attachments with an XHR object, it is
possible to create an <iframe> and use a standard form upload to target this location. To
make things very easy for developers, these details can be hidden in the library. Using the
sendRequest’s serializeForm option on a form that contains a file upload field, the
iframe will be created, the form target set, and the form submitted. The whole process will
work just fine without a full page postback. You can see that nothing special is required by
viewing the example at http://ajaxref.com/ch5/fileuploadrevisited.html.

NNOT EOTE Because iframes are used here, some of the ideas that are unique to XHRs such as monitoring
status will not be natively handled with file uploads.

Request Status

As you saw in Chapter 3, the XHR goes through various readyState values indicating the
status of the request. We showed earlier that these were made constants in the library. The
library also allows us to define special callbacks to be invoked upon each state, as follows:


  • onCreate Called when an XHR is first created (readyState 0)

  • onOpen Called after the open() method is invoked on the XHR (readyState 1)

  • onSent Called once the request has been sent but before data is received
    (readyState 2)

  • onLoading Called as data is loading (readyState 3)

  • onReceived Called when the call is complete no matter the status (readyState 4)


The example here shows all the readyState values for a request as it goes along. It can
be accessed at http://ajaxref.com/ch5/readystaterevisited.html.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
Free download pdf