Core · jQuery-compatible

$.get, $.post & $.getJSON

The one-liners. await $.getJSON('/api/users.json') and you have data; $.post() sends a form-encoded body; $.get() returns text or JSON based on the response.

Each shorthand returns the same Promise as $.ajax(). For $.get() and $.getJSON(), an object in the second argument becomes the query string; for $.post() it becomes an application/x-www-form-urlencoded body. A callback in the last position still works for older code.

Live · Team directory + quote of the day

View the source for this demo
HTML
<div class="row">
  <button type="button" id="fs-load">Load team ($.getJSON)</button>
  <button type="button" id="fs-sort">Sort by commits</button>
  <button type="button" id="fs-quote">New quote ($.get)</button>
</div>
<div class="team" id="fs-team" aria-live="polite"></div>
<p id="fs-q" style="margin-top:14px;min-height:1.6em" aria-live="polite"></p>
JavaScript
let team = [];
const render = () => $('#fs-team').html(team.map(u =>
  '<div class="p"><b>' + $.escape(u.name) + '</b><small>' + $.escape(u.role) + ' · ' + u.commits + ' commits</small></div>').join(''));

$('#fs-load').on('click', async () => {
  team = await $.getJSON('/api/users.json');
  render();
  $.from('#fs-team .p', { y: 14, opacity: 0, duration: .3, stagger: .06 });
});

$('#fs-sort').on('click', () => {
  team.sort((a, b) => b.commits - a.commits);
  render();
});

$('#fs-quote').on('click', async () => {
  const n = 1 + Math.floor(Math.random() * 5);
  $('#fs-q').html(await $.get('/api/quotes/' + n + '.html'));
});

More examples

Shorthands

JavaScript
const users = await $.getJSON('/api/users', { team: 'core' });   // ?team=core
const html  = await $.get('/partials/sidebar.html');
await $.post('/api/subscribe', { email });
$.get('/api/ping', data => console.log(data));                    // callback style

API

$.get(url [, data] [, success]) → Promise

GET shorthand; data becomes the query string.

Reference: $.get() →
$.post(url [, data] [, success]) → Promise

POST shorthand (urlencoded body by default).

Reference: $.post() →
$.getJSON(url [, data] [, success]) → Promise

GET with forced JSON parsing.

Reference: $.getJSON() →

Migrating

Coming from jQuerySame signatures. $.getScript() is not included — use a dynamic import() or a script element.

Related features