jQuery AJAX with JSON allows web pages to communicate with servers asynchronously and exchange data in JSON format without reloading the page.
The $.ajax() and $.getJSON() methods are commonly used to fetch JSON data from servers.
// Fetch JSON data using jQuery AJAX
$.ajax({
url: "https://jsonplaceholder.typicode.com/users",
method: "GET",
dataType: "json",
success: function(data) {
console.log(data);
}
});
The AJAX call fetches user data in JSON format and logs it to the browser console without refreshing the page.
// Load JSON data and display it dynamically
$("#loadData").click(function() {
$.getJSON("https://jsonplaceholder.typicode.com/users", function(users) {
let html = "<ul>";
users.forEach(function(user) {
html += "<li>" + user.name + "</li>";
});
html += "</ul>";
$("#result").html(html);
});
});