🔹 1. Why Use AJAX with
jQuery?
AJAX (Asynchronous JavaScript and XML) allows you to send
and receive data without reloading the page. jQuery simplifies AJAX
calls to just a few lines of code, making dynamic, real-time apps more accessible.
✅ With jQuery AJAX, you can:
🔹 2. jQuery’s AJAX
Shortcuts
✅ .load() — Load HTML into an
element
$("#result").load("data.html");
✅ .get() — Simple GET request
$.get("/api/user",
function(data){
console.log(data);
});
✅ .post() — Send data via POST
$.post("/api/login",
{ username: "john", password: "123" }, function(response){
alert("Logged in!");
});
🔹 3. Using .ajax() for
Full Control
$.ajax({
url: "/api/data",
type: "GET",
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(err) {
console.error("Error loading
data:", err);
}
});
|
Property |
Purpose |
|
url |
API or file
to fetch |
|
type |
Request
method: GET/POST/PUT/DELETE |
|
dataType |
Expected
response type: json, html, etc. |
|
success |
Callback on
success |
|
error |
Callback on
failure |
🔹 4. Sending Data via
POST
let
formData = {
name: $("#name").val(),
email: $("#email").val()
};
$.post("/api/signup",
formData, function(response) {
alert("Signup complete!");
});
✅ Always validate input before
sending to the server.
🔹 5. Handling JSON
Responses
$.get("/api/users",
function(data){
data.forEach(function(user){
$("#users").append("<li>" + user.name +
"</li>");
});
});
|
Data
Format |
Use Case |
|
JSON |
APIs, server
objects |
|
HTML |
Page
fragments |
|
Text |
Simple
messages |
🔹 6. AJAX with Loading
Spinners
$("#loadBtn").on("click",
function(){
$("#spinner").show();
$.get("/api/products",
function(data){
$("#productList").html(data);
}).always(function(){
$("#spinner").hide();
});
});
✅ Use .always() to hide loaders
whether request succeeds or fails.
🔹 7. Submitting Forms via
AJAX
$("#loginForm").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "/api/login",
data: $(this).serialize(), // Converts form
fields into query string
success: function(response){
alert("Login success!");
},
error: function(){
alert("Login failed.");
}
});
});
✅ .serialize() handles input
fields automatically.
🔹 8. Error Handling
$.get("/api/invalid-url")
.done(function(data){ console.log(data); })
.fail(function(jqXHR, status, error){
console.error("Request failed:",
status);
});
Use .fail() or the error: callback to capture errors and
notify users gracefully.
🔹 9. Dynamic Content
Update Example
HTML:
<button
id="loadUsers">Load Users</button>
<ul
id="userList"></ul>
jQuery:
$("#loadUsers").click(function(){
$.getJSON("/api/users",
function(users){
$("#userList").empty();
users.forEach(function(user){
$("#userList").append("<li>" + user.name +
"</li>");
});
});
});
🔹 10. Summary Table: AJAX
Essentials
|
Task |
jQuery
Code Example |
|
Load HTML |
$("#div").load("file.html") |
|
Simple GET |
$.get("/api",
callback) |
|
Simple POST |
$.post("/submit",
{name: "John"}) |
|
Full AJAX
Call |
$.ajax({...}) |
|
Handle JSON |
$.getJSON("/api",
function(data){}) |
|
Show loader |
$("#loader").show()
before request |
|
Form
submission |
.serialize()
with .submit() |
jQuery is a fast, small JavaScript library that simplifies HTML document traversal, manipulation, event handling, and AJAX.
Yes — while modern frameworks exist, jQuery remains heavily used in WordPress, older projects, CMSs, and quick prototypes.
It shortens code, handles cross-browser issues, simplifies AJAX, and is beginner-friendly.
<script
src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load.
$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load
✅ Yes — it’s open-source under the MIT license.
jQuery is a library built with JavaScript to simplify tasks like DOM handling and AJAX.
✅ Yes — it works across all major desktop and mobile browsers.
It’s better to learn JavaScript fundamentals first, but jQuery is easier to grasp early on for quick UI work.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)