jQuery in Action

252
CHAPTER 8
Talk to the server with Ajax
That's a lot of options to keep track of, but it's unlikely that more than a few of
them will be used for any one request. Even so, wouldn't it be convenient if we
could set default values for these options for pages where we're planning to make
a large number of requests?
8.4.2 Setting request defaults
Obviously the last question in the previous section was a setup. As you might have
suspected, jQuery provides a way for us to set up a default set of Ajax properties
that will be used when we don't override their values. This can make pages that
initiate lots of similar Ajax calls much simpler.
The function to set up the list of Ajax defaults is
$.ajaxSetup()
, and its syntax
is as follows:
At any point in script processing, usually at page load (but can be at any point of
the page authors' choosing), this function can be used to set up defaults to be
used for all subsequent calls to
$.ajax()
.
NOTE
Defaults set with this function aren't applied to the
load()
command. For
utility functions such as
$.get()
and
$.post()
, the
HTTP
method can't
be overridden by use of these defaults. Setting a default
type
of
GET
won't
cause
$.post()
to use the
GET
HTTP
method.
Let's say that we are setting up a page where, for the majority of Ajax requests
(made with the utility function rather than the
load()
command), we want to set
up some defaults so that we don't need to specify them on every call. We can, as
the first statement in the header
<script>
element, write
$.ajaxSetup({
type: 'POST',
timeout: 5000,
Command syntax: $.ajaxSetup
$.ajaxSetup(properties)
Establishes the passed set of properties as the defaults for subsequent calls to $.ajax().
Parameters
properties
(Object) An object instance whose properties define the set of default Ajax
properties. These are the same properties described for the $.ajax()
function in table 8.2.
Returns
Undefined.


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

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