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

JK JK 2038 
Created at
Updated at  
8,242 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,861
  1. Loading XML Data with JavaScript
    7,604
  2. Difference between Java and Javascript
    7,756
  3. Regular Expressions in JavaScript
    7,727
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    935
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    825
  3. Michael Jackson's Billie Jean
    401
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    377
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    338
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    371
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    378
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    397
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,305
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    552
  11. The Evolution and Production Reality of Agentic AI
    430
  12. How to Activate or Waive Your UIUC Student Health Insurance
    546
  13. Complete Guide to Building a Machine Learning Model
    565
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,614
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    589
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,364
  17. Clean Python Environments: The Power of venv vs. Docker
    1,109
  18. What is Docker? Why is Docker also useful in a development environment?
    821
  19. UIUC 2026-2027 Academic Calendar
    2,049
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,080
  21. Open-Source LLMs: The AI Revolution
    988
  22. Resume 2.0: Leveling Up for My First Software Gig
    3,014
  23. Not everyone will understand what this man just did
    2,473
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,872
  25. Unpacking IU's Shopper
    983
  26. Jackie Chan's Police Story: The Action Masterpiece
    871
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,210
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,399
  29. Open Databases for Sex Crime Occurrences in the U.S.
    983
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,436
  31. My First Day at University of Illinois-Urvana Champaign
    1,379
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,430
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,496
  34. Sipping the Stars: My Starbucks Adventure
    10,169
  35. Exciting explore at Sequoia National Park
    7,868
  36. My Life Shot at Death Valley
    2,070
  37. Ip Man fights with Muay Thai Master
    1,139
  38. Mad Clown - Don't Die
    1,261
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,931
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,095
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,362
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,153
  43. 10CM - To Reach You (너에게 닿기를)
    1,331
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,749
  45. BOYNEXTDOOR - If I Say I Love You
    1,384
  46. The Future of Software Engineer - AI Engineering
    1,107
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,117
  48. Lie - Legend song by BIGBANG
    8,005
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,106
  50. Reimbursement after Vaccination at McKinley Health Center
    1,228