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

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