SyntaxStudy
Sign Up
jQuery jQuery $.get() and $.post()
jQuery Beginner 5 min read

jQuery $.get() and $.post()

For the most common HTTP operations jQuery provides the convenience methods $.get() and $.post(), which require far less configuration than the full $.ajax() call while still returning a jqXHR promise.

$.get()

$.get(url, data, callback, dataType) performs a GET request. The data object is serialised to a query string and appended to the URL. Use it to fetch resources, search results, or filtered lists from a REST API.

$.post()

$.post(url, data, callback, dataType) sends data in the request body as application/x-www-form-urlencoded. Use it to submit forms, create resources, or trigger server-side actions.

  • Both methods return a jqXHR object for promise chaining
  • Pass 'json' as the last argument to auto-parse the response
  • Query string parameters are URL-encoded automatically

Because GET requests are cached by browsers and appear in server logs, never use them to send sensitive data like passwords. Always use POST (or PUT/PATCH/DELETE via $.ajax()) for mutations.

Example
// GET request with query parameters
$.get('/api/search', { q: 'jquery', page: 1 }, function (results) {
    results.forEach(function (item) {
        $('#results').append('<li>' + item.title + '</li>');
    });
}, 'json');

// POST request — submit a form via AJAX
$('#contact-form').on('submit', function (e) {
    e.preventDefault();
    $.post('/api/contact', $(this).serialize(), function (resp) {
        $('#message').text(resp.message);
    }, 'json')
    .fail(function () {
        $('#message').text('Something went wrong. Please try again.');
    });
});
Pro Tip

Use $(form).serialize() to convert all form fields into a URL-encoded string ready for $.post().