jQuery AJAX

jQuery AJAX GET and POST Examples

Compare jQuery AJAX GET and POST requests and learn how to send form and JSON data.

jQuery AJAX GET and POST Examples

GET requests usually retrieve data, while POST requests submit data or initiate an action. Choose the method according to the endpoint contract rather than convenience.

GET with query data

$.get('/api/search.php', { q: 'ajax' }, function (data) {
  console.log(data);
}, 'json');

POST form data

$.post('/api/contact.php', {
  name: $('#name').val(),
  message: $('#message').val()
}, function (result) {
  $('#feedback').text(result.message);
}, 'json').fail(function (xhr) {
  $('#feedback').text('The request could not be completed.');
});

JSON request bodies

When an endpoint expects JSON, use $.ajax() with contentType: 'application/json' and JSON.stringify(payload). Ordinary jQuery form submissions are typically encoded as form data instead.

Validate all fields on the server, use CSRF protection for state changes, and do not trust client-side validation as a security boundary.

Share this page Help other developers discover this resource.