jQuery Example to make GET method call with $.ajax()

JK 2002 
Created at
Updated at  
7,950 0 0

Below is the example to call certain URL with $.ajax()

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery AJAX Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
    $("#loadDataBtn").click(function() {
        $.ajax({
            url: "https://api.example.com/data", // URL to your API endpoint
            type: "GET", // HTTP method (GET, POST, PUT, DELETE, etc.)
            dataType: "json", // Expected data type from the server
            success: function(data) {
                // Callback function executed if the request succeeds
                // 'data' contains the response from the server
                console.log("Data loaded successfully:", data);
                // You can process the 'data' here, for example, display it on the webpage
                $("#result").html("<pre>" + JSON.stringify(data, null, 2) + "</pre>");
            },
            error: function(xhr, status, error) {
                // Callback function executed if the request fails
                console.error("Request failed:", status, error);
                // You can handle errors here, for example, display an error message
                $("#result").text("Error loading data. Please try again later.");
            }
        });
    });
});
</script>
</head>
<body>

<button id="loadDataBtn">Load Data</button>

<div id="result"></div>

</body>
</html>

In this example:

  • The jQuery library is included via a CDN.
  • The JavaScript code is enclosed within $(document).ready() to ensure it executes after the DOM is fully loaded.
  • When the button with the ID loadDataBtn is clicked, an AJAX request is initiated using $.ajax().
  • The request is a GET request to the URL "https://api.example.com/data".
  • The dataType option is set to "json", indicating that we expect JSON data in response.
  • If the request is successful, the success callback function is executed, where you can handle the response data.
  • If the request fails, the error callback function is executed, where you can handle errors.
  • In this example, the response data is displayed within the <div id="result">.


Associated Data for the jQuery AJAX GET Example:

  • jQuery AJAX: A powerful JavaScript library that provides a convenient way to make asynchronous HTTP requests to web servers.
  • $.ajax(): A core function within jQuery that handles AJAX requests.
  • GET Request: An HTTP method commonly used to retrieve data from a server.
  • API Endpoint: A specific URL that represents a resource available through an API.
  • JSON (JavaScript Object Notation): A lightweight data-interchange format commonly used for data transmission on the web.
  • Callback Functions: Functions that are executed in response to specific events, like the success or failure of an AJAX request.
  • DOM (Document Object Model): A programming interface for HTML documents that represents the structure and content of a web page.
  • CDN (Content Delivery Network): A network of servers distributed globally that can deliver content (like libraries) more efficiently.

Additional Notes:

  • The code in the example assumes the existence of a hypothetical API endpoint at "https://api.example.com/data".
  • The response data is displayed in a `` tag to preserve formatting.
  • Error handling is implemented using the `error` callback function.
  • The example illustrates a basic implementation of an AJAX GET request. More advanced techniques exist for handling various scenarios, such as data serialization, authentication, and error handling.
Tags $.ajax() jQuery Facebook X
Comments 0
Similar posts
  1. Building a Brighter Future: Launching My First GitHub Repository for WE Service
    7,642
  1. Loading XML Data with JavaScript
    7,439
  2. Difference between Java and Javascript
    7,563
  3. Regular Expressions in JavaScript
    7,539
Recently updated
  1. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    31
  2. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    160
  3. The Evolution and Production Reality of Agentic AI
    158
  4. How to Activate or Waive Your UIUC Student Health Insurance
    236
  5. Complete Guide to Building a Machine Learning Model
    280
  6. My life cuts at Las Vegas during Thanksgiving day holiday
    7,361
  7. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    305
  8. Harness vs. OpenClaw: Two Very Different "Agents"
    885
  9. Clean Python Environments: The Power of venv vs. Docker
    758
  10. What is Docker? Why is Docker also useful in a development environment?
    578
  11. UIUC 2026-2027 Academic Calendar
    1,450
  12. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    759
  13. Open-Source LLMs: The AI Revolution
    704
  14. Resume 2.0: Leveling Up for My First Software Gig
    2,042
  15. Not everyone will understand what this man just did
    1,703
  16. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,539
  17. Unpacking IU's Shopper
    703
  18. Jackie Chan's Police Story: The Action Masterpiece
    599
  19. The IVE Story: Identity, 'I AM' Charts, and Influence
    901
  20. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,143
  21. Open Databases for Sex Crime Occurrences in the U.S.
    671
  22. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,500
  23. My First Day at University of Illinois-Urvana Champaign
    1,152
  24. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,079
  25. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,151
  26. Sipping the Stars: My Starbucks Adventure
    9,560
  27. Exciting explore at Sequoia National Park
    7,613
  28. My Life Shot at Death Valley
    1,613
  29. Ip Man fights with Muay Thai Master
    896
  30. Mad Clown - Don't Die
    970
  31. How to get Student Enrollment and Degree Verification at UIUC
    4,627
  32. LAX Thanksgiving Rush: A Joyful Reunion
    891
  33. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,075
  34. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    938
  35. 10CM - To Reach You (너에게 닿기를)
    1,113
  36. Feeling weak? Transform yourself at the UIUC ARC!
    1,543
  37. BOYNEXTDOOR - If I Say I Love You
    1,149
  38. The Future of Software Engineer - AI Engineering
    909
  39. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    875
  40. Lie - Legend song by BIGBANG
    7,786
  41. Why ROLLBACK is useful when you work with Google Gemini CLI?
    803
  42. Reimbursement after Vaccination at McKinley Health Center
    969
  43. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    936
  44. Common Questions from UIUC school life in terms of CS Program
    1,067
  45. UIUC Immunization Compliance
    1,164
  46. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,058
  47. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,057
  48. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    841
  49. Common methods to improve coding skills
    956
  50. US National Holiday in 2026
    873