← Back to Chapters

jQuery AJAX

? jQuery AJAX

✨ Quick Overview

jQuery AJAX allows web pages to communicate with servers asynchronously without reloading the page. It simplifies HTTP requests such as GET, POST, PUT, and DELETE using easy-to-use methods.

? Key Concepts

  • Asynchronous communication with servers
  • Partial page updates
  • JSON, HTML, or text data exchange
  • Client-side interaction without refresh

? Syntax / Theory

The core method is $.ajax(), which provides full control over the request lifecycle.

? View Code Example
// Basic jQuery AJAX syntax
$.ajax({
url: "server.php",
type: "GET",
success: function(response) {
console.log(response);
}
});

? Code Example(s)

? View Code Example
// Load data from a public API using jQuery AJAX
$("#loadBtn").click(function() {
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
success: function(data) {
$("#result").text(JSON.stringify(data, null, 2));
}
});
});

? Live Output / Explanation

When the button is clicked, an AJAX request is sent to the API. The response is received as JSON and displayed instantly without reloading the page.

? Interactive Example

Click the button above to load data...

? Use Cases

  • Loading data dynamically
  • Form submission without reload
  • Search suggestions
  • Real-time dashboards

✅ Tips & Best Practices

  • Always handle error callbacks
  • Use JSON for structured data
  • Avoid blocking UI operations

? Try It Yourself

  • Change the API URL and observe results
  • Use POST instead of GET
  • Add an error handler