jQuery in Action

224
CHAPTER 8
Talk to the server with Ajax
Instantiating an
XHR
object requires browser-specific code.
Ready handlers need to sift through a lot of uninteresting state changes.
Ready handlers don't automatically get a reference to invoking
XHR
instances.
The response body needs to be dealt with in numerous ways depending
upon its format.
The remainder of this chapter will describe how the jQuery Ajax commands and
utility functions make Ajax a lot easier (and cleaner) to use on our pages. There
are a lot of choices in the jQuery Ajax
API
, and we'll start with some of the sim-
plest and most often-used tools.
8.2 Loading content into elements
Perhaps one of the most common uses of Ajax is to grab a chunk of content from
the server and stuff it into the
DOM
at some strategic location. The content
could be an
HTML
fragment that's to become the child content of a target con-
tainer element, or it could be plain text that will become the content of the tar-
get element.
Let's imagine that, on page load, we want to grab a chunk of
HTML
from the
server using a resource named
/serverResource
and make it the content of a
Figure 8.1 The life cycle of an Ajax request as it makes its way from the client to the server
and back again


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.