02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

82<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

Figure 2-24. The result after overriding the default type option with GET<br />

Using Shorth<strong>and</strong> AJAX Methods<br />

There are several simple, one-use functions, available for performing common AJAX tasks. In a nutshell,<br />

these shorth<strong>and</strong> methods are simply wrapper functions that call $.ajax() with some of the parameters<br />

already set.<br />

Using these methods will incur a slight performance penalty, since you’re essentially calling a<br />

method that sets up parameters <strong>and</strong> calls $.ajax() within itself. However, the convenience of using<br />

shorth<strong>and</strong> methods really speeds up development in many scripts.<br />

$.get() <strong>and</strong> $.post()<br />

For st<strong>and</strong>ard GET <strong>and</strong> POST requests, the $.get() <strong>and</strong> $.post() functions are easy to use. Both take four<br />

arguments: the URL to which the request is to be sent, optional data to be sent to the remote script, an<br />

optional callback to be executed if the request is successful, <strong>and</strong> an optional dataType setting.<br />

To load the result of ajax.php using GET with no data sent, use the following:<br />

$.get("ajax.php", function(data){<br />

$("#bar")<br />

.css("background","yellow")<br />

.html(data);<br />

});

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!