jQuery in Action

Loading content into elements
227
Though simple to use, this command has some important nuances. For example,
when the
parameters
parameter is used to supply the request parameters, the
request is made using the
POST
HTTP
method; otherwise, a
GET
request is initi-
ated. If we want to make a
GET
request with parameters, we can include them as a
query string on the
URL
. But be aware that when we do so, we're responsible for
ensuring that the query string is properly formatted and that the names and val-
ues of the request parameters are
URI
-encoded.
Most of the time, we'll use the
load()
command to inject the complete
response into whatever elements are contained within the wrapped set, but some-
times we may want to filter elements coming back as the response. If we want to
filter response elements, jQuery allows us to specify a selector on the
URL
that
will be used to limit which response elements are injected into the wrapped ele-
ments by suffixing the
URL
with a space and pound sign character (#) followed
by the selector.
For example, to filter response elements so that only
<div>
instances are
injected, we write
$('.injectMe').load('/someResource #div');
If the request parameters come from form controls, a helpful command in build-
ing a query string is
serialize()
, whose syntax is as follows:

Command syntax: load
load(url,parameters,callback)
Initiates an Ajax request to the specified URL with optional parameters. A callback function
can be specified that's invoked when the request completes. The response text replaces the
content of all matched elements.
Parameters
url
(String) The URL of the server-side resource to which the request is sent.
parameters
(Object) An object whose properties are serialized into properly encoded
parameters to be passed to the request. If specified, the request is made
using the POST method. If omitted, the GET method is used.
callback
(Function) A callback function invoked after the response data has been
loaded into the elements of the matched set. The parameters passed to
this function are the response text, the status code, and the XHR instance.
Returns
The wrapped set.


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

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