Web Development with jQuery®
elliott
(Elliott)
2019-07-27 03:07:34 UTC
#1
(^188) ❘ CHAPTER 7 AJAX
element with a for attribute having the value addressState. Then you set that
ele-
ment’s text content to the text content of the
element from the XML document.
// Change the label
$('label[for="addressState"]').text(
xml.find('label').text() + ':'
);
As you can see with the preceding example, jQuery does not disappoint with its well-thought-out
AJAX-handling abilities. With traditional JavaScript and DOM methods, the preceding would have
been much more verbose and much more diffi cult to get working. jQuery’s capability to bind itself
to an XML response makes parsing and working with XML documents just like working with
HTML documents: easy.
NOTE The iTunes-like fl ags included in the source code download originated
from the following website, where you may also obtain higher-quality images:
[http://www.bartelme.at/journal/archive/flag_button_devkit/](http://www.bartelme.at/journal/archive/flag_button_devkit/)
Sending Data Along with a Request
Let’s say in that last example that you were actually working with a database-driven server; in that
case, how you would have constructed the request would be slightly different than in the preceding
example. Instead of dynamically creating the fi lename of the XML fi le you want to retrieve using
the country’s id, you would instead need to pass that information separately. jQuery accommodates
passing data in the $.get() method. In the context of the preceding example, you started out mak-
ing a call to the $.get() method that looked like this, with the extra code snipped out to make the
example easier to understand:
$.get(
'Example 7-1/38.xml',
function(xml)
{
// snip
},
'xml'
);
The fi rst argument is the path of the fi le you’re requesting—this can be any URL value. Typically, you’ll
want to reference some server-side script that can output data for you. The second argument is the
callback function that the server’s response XML will be passed to, and the third argument is the type
of request being made, which is one of the following strings: 'xml', 'html', 'script', 'json', 'jsonp', or
'text'. This argument is set depending on the type of data that you expect coming back from the server.
When you want to send additional data with the request, another argument is added:
$.get(
'Example 7-1/38.xml', {
[http://www.it-ebooks.info](http://www.it-ebooks.info)
← Previous
Next →
Free download pdf