Skip to content
jQuery

AJAX Requests

Use $.ajax, $.get, $.post, and load with promises.

#ajax#http

Code

jquery
// Promise-based $.ajax
$.ajax({
  url: "/api/users",
  method: "GET",
  dataType: "json"
})
  .done(data => console.log("users", data))
  .fail((xhr, status) => console.error("error", status));

// Shorthands
$.get("/api/posts", posts => render(posts));
$.post("/api/posts", { title: "Hi" }, post => console.log(post));

// JSON-P and loading HTML into an element
$("#result").load("/fragment.html #content");