jQuery Tutorial
jQuery Introduction jQuery Getting Started jQuery Syntax jQuery Selectors jQuery Events
jQuery Effects
jQuery Hide/Show jQuery Fade jQuery Slide jQuery Animation jQuery stop() jQuery Callback jQuery Chaining
jQuery Manipulation
jQuery Get/Set jQuery Add jQuery Remove jQuery CSS Classes jQuery css() jQuery Dimensions
jQuery Traversing
jQuery Traversing jQuery Ancestors jQuery Descendants jQuery Siblings jQuery Filtering
jQuery AJAX
jQuery AJAX Intro jQuery Load jQuery Get/Post jQuery AJAX Events
jQuery Misc
jQuery noConflict() jQuery Filters
jQuery References
jQuery Reference jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Miscellaneous jQuery Properties

jQuery - AJAX get() and post() Methods

The jQuery $.get() and $.post() methods are used to request data from the server with an HTTP GET and POST request.


HTTP Request: GET vs. POST

In GET method the browser will add the name/value pairs to the end of the URL.

GET is generally used where security is not a concern.

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions (only 2048 characters)

In POST method contents are not displayed in the URL.

You should always use POST if the form data contains sensitive or personal information.

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests should be used when dealing with sensitive data
  • POST requests have no restrictions on data length

To learn more about GET and POST, and the differences between the two method, visit our HTTP Request Methods page.


jQuery $.get() Method

The jQuery $.get() method loads data from the server using a HTTP GET request.

Here is the syntax for $.get() method:

$.get(URL, data, callback)

Parameters:

  • URL - Specifies the URL you wish to request
  • data - (Optional) Specifies a plain object or string that is send to the server along with the request
  • callback - (Optional) Specifies a callback function that is executed if the request succeeds

This example requests the ajax_get.php page, send some additional data along and alert status message:

Run code

Here is how the PHP file looks like ("ajax_get.php"):

<?php
echo "<p>Hello ".$_GET['fname']." ".$_GET['lname'].", How are u doing?</p>";
?>


jQuery $.post() Method

The jQuery $.post() method loads data from the server using a HTTP POST request.

Here is the syntax for $.post() method:

$.post(URL, data, callback)

Parameters:

  • URL - Specifies the URL you wish to request
  • data - (Optional) Specifies a plain object or string that is send to the server along with the request
  • callback - (Optional) Specifies a callback function that is executed if the request succeeds

This example requests the ajax_post.php page, send some additional data along and alert status message:

Run code

Here is how the PHP file looks like ("ajax_post.php"):

<?php
echo "<p>Hello ".$_POST['fname']." ".$_POST['lname'].", How are u doing?</p>";
?>

jQuery AJAX Reference

For a complete AJAX methods reference, visit our jQuery AJAX Reference.