jQuery in Action

Making GET and POST requests
237
JavaScript data item available to its callback. The syntax of this utility function is
as follows:
This function is great for those times when we want to get data from the server
without the overhead of dealing with
XML
. Let's see an example that puts it to
work for us.
Loading cascading dropdowns
When creating Rich Internet Applications, we often encounter the need to set the
options in a dropdown control to values that are dependent on the setting of
some other control, frequently another dropdown. A common example is choos-
ing a state or province from one dropdown that causes a subsequent dropdown to
be loaded with the list of cities in that state or province.
Such a set of controls has come to be known by the term dependent dropdowns--
or, sometimes, cascading dropdowns--and has become a poster child for Ajax; it's
used as an example in almost every Ajax book in existence, as well as all over the
Internet. In this section, we'll look at how to solve this canonical problem and cre-
ate a set of elements by leveraging the jQuery
$.getJSON()
utility function.
For this example, we'll return to The Boot Closet page that we set up in
section 8.2.2 and extend its capabilities. As originally written, the page allows
our customers to find out which boots are available and the detailed informa-
tion associated with the available boots, but they have no way to pick a pair for
Command syntax: $.getJSON
$.getJSON(url,parameters,callback)
Initiates a GET request to the server using the specified URL with any passed parameters as
the query string. The response is interpreted as a JSON string, and the resulting data is
passed to the callback function.
Parameters
url
(String) The URL of the server-side resource contacted via the GET method.
parameters
(Object|String) An object whose properties serve as the name/value pairs
used to construct a query string to be appended to the URL, or a preformat-
ted and encoded query string.
callback
(Function) A function invoked when the request completes. The data value
resulting from digesting the response body as a JSON string is passed as
the first parameter to this callback, and the status as the second.
Returns
The XHR instance.


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

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