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

JK JK 2026 
Created at
Updated at  
8,071 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,730
  1. Loading XML Data with JavaScript
    7,509
  2. Difference between Java and Javascript
    7,646
  3. Regular Expressions in JavaScript
    7,603
Recently updated
  1. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    119
  2. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    123
  3. Watch This Dazzling Dancer’s Mind-Blowing Moves
    153
  4. Michael Jackson's Billie Jean
    222
  5. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    191
  6. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    615
  7. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    316
  8. The Evolution and Production Reality of Agentic AI
    276
  9. How to Activate or Waive Your UIUC Student Health Insurance
    354
  10. Complete Guide to Building a Machine Learning Model
    386
  11. My life cuts at Las Vegas during Thanksgiving day holiday
    7,475
  12. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    413
  13. Harness vs. OpenClaw: Two Very Different "Agents"
    1,057
  14. Clean Python Environments: The Power of venv vs. Docker
    897
  15. What is Docker? Why is Docker also useful in a development environment?
    671
  16. UIUC 2026-2027 Academic Calendar
    1,698
  17. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    868
  18. Open-Source LLMs: The AI Revolution
    821
  19. Resume 2.0: Leveling Up for My First Software Gig
    2,395
  20. Not everyone will understand what this man just did
    2,000
  21. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,655
  22. Unpacking IU's Shopper
    829
  23. Jackie Chan's Police Story: The Action Masterpiece
    709
  24. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,018
  25. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,239
  26. Open Databases for Sex Crime Occurrences in the U.S.
    770
  27. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,810
  28. My First Day at University of Illinois-Urvana Champaign
    1,248
  29. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,207
  30. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,288
  31. Sipping the Stars: My Starbucks Adventure
    9,757
  32. Exciting explore at Sequoia National Park
    7,724
  33. My Life Shot at Death Valley
    1,785
  34. Ip Man fights with Muay Thai Master
    994
  35. Mad Clown - Don't Die
    1,089
  36. How to get Student Enrollment and Degree Verification at UIUC
    5,082
  37. LAX Thanksgiving Rush: A Joyful Reunion
    963
  38. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,183
  39. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,032
  40. 10CM - To Reach You (너에게 닿기를)
    1,208
  41. Feeling weak? Transform yourself at the UIUC ARC!
    1,617
  42. BOYNEXTDOOR - If I Say I Love You
    1,230
  43. The Future of Software Engineer - AI Engineering
    970
  44. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    963
  45. Lie - Legend song by BIGBANG
    7,864
  46. Why ROLLBACK is useful when you work with Google Gemini CLI?
    897
  47. Reimbursement after Vaccination at McKinley Health Center
    1,073
  48. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,010
  49. Common Questions from UIUC school life in terms of CS Program
    1,177
  50. UIUC Immunization Compliance
    1,269