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

JK1912 
Created at Mar 14, 2024 16:07:05
Updated at Oct 03, 2024 05:01:02 
  6,789   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 Share on Facebook Share on X

◀ PREVIOUS
Difference between Java and Javascript

▶ NEXT
Loading XML Data with JavaScript

  Comments 0
SIMILAR POSTS

Building a Brighter Future: Launching My First GitHub Repository for WE Service

(updated at Sep 22, 2024)


OTHER POSTS IN THE SAME CATEGORY

Loading XML Data with JavaScript

(updated at Oct 03, 2024)

Difference between Java and Javascript

(updated at Oct 03, 2024)

Regular Expressions in JavaScript

(updated at Oct 03, 2024)

UPDATES

The Future of Software Engineer - AI Engineering

(updated at Nov 05, 2025)

G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025

(updated at Nov 01, 2025)

Lie - Legend song by BIGBANG

(updated at Nov 01, 2025)

Why ROLLBACK is useful when you work with Google Gemini CLI?

(created at Oct 24, 2025)

Reimbursement after Vaccination at McKinley Health Center

(created at Oct 24, 2025)

Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!

(created at Oct 21, 2025)

Common Questions from UIUC school life in terms of CS Program

(created at Oct 20, 2025)

UIUC Immunization Compliance

(created at Oct 20, 2025)

LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...

(created at Oct 18, 2025)

LEE CHANHYUK - Endangered Love (멸종위기사랑)

(created at Oct 18, 2025)

Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)

(created at Oct 18, 2025)

Common methods to improve coding skills

(created at Oct 18, 2025)

US National Holiday in 2026

(created at Oct 18, 2025)

BABYMONSTER “WE GO UP” Band LIVE [it's Live] K-POP live music show

(created at Oct 18, 2025)

BLACKPINK - ‘Shut Down’ Live at Coachella 2023

(created at Oct 18, 2025)

JENNIE - like JENNIE - One of Hot K-POP in 2025

(created at Oct 18, 2025)

BABYMONSTER(베이비몬스터) - DRIP + HOT SOURCE + SHEESH

(created at Oct 08, 2025)

Common Naming Format in Software Development

(created at Oct 07, 2025)

In a life where I don't want to spill even a single sip of champagne - LEE CHANHYUK - Panorama(파노라마)

(created at Oct 06, 2025)

Countries with more males and females - what about UIUC?

(created at Oct 04, 2025)

Challenge: One Code Problem Per Day

(created at Oct 03, 2025)

Urban planning and growth from a historical perspective

(created at Sep 28, 2025)

Jackbryan VS Serpent | Korea Beatbox Championship 2023 | Quarterfinal

(created at Sep 28, 2025)

CNBLUE - You've Fallen For Me (넌 내게 반했어)

(created at Sep 28, 2025)

GGIS: The Roots of Visualizing Geographic Information

(created at Sep 27, 2025)

CNBLUE - 외톨이야 (aka Outsider)

(created at Sep 27, 2025)

Did you know that the person who voiced Humtrix Rumi in KPop Demon Hunters went to UIUC?

(updated at Sep 05, 2025)

WING - Dopamine

(created at Sep 05, 2025)

CARDIO VS Jackbryan | Korea Beatbox Championship 2025 | Semifinal

(updated at Sep 04, 2025)

Tech Visionaries who graduated at UIUC - You are the Next Turn

(updated at Sep 04, 2025)

Thinking about the Public Dataset and Open API provided for the Authorized People

(updated at Sep 04, 2025)

Where to Eat with Your i-Card at UIUC and How to Track Your Dining Dollars

(updated at Sep 04, 2025)

OMG! Did you consume your meals already at UIUC? How do you change the meal plan?

(updated at Sep 03, 2025)

Java Comments

(updated at Sep 03, 2025)

Abraham Lincoln Contributed to UIUC's Creation and its Mission

(created at Sep 03, 2025)

Feeling weak? Transform yourself at the UIUC ARC!

(updated at Sep 03, 2025)

Checking Your Upcoming Assignment/Exam Schedule: Using the UIUC Canvas Dashboard for Assignment Management

(updated at Sep 03, 2025)

UIUC Course Map for CS and Blended CS Degrees

(updated at Sep 02, 2025)

What You Need to Prepare for Graduate University at UIUC

(updated at Sep 01, 2025)

Did you know about the UIUC Course Numbering Policy? How to meet with 120 GPA hours?

(created at Sep 01, 2025)

My Dad's Bucket Hat Craze: One Man's Quest for Collegiate Headwear

(created at Aug 30, 2025)

Public Transportation between Chicago O'Hare International Airport and UIUC (University of Illinois at Urbana-Champaign)

(updated at Aug 27, 2025)

How to Receive Mail and Packages in University Housing at UIUC

(updated at Aug 27, 2025)

When you are too busy to have your breakfast/lunch/dinner, use Good2Go Carryout Program

(created at Aug 27, 2025)

Why Outlook’s Redirection Option Is a Game-Changer

(updated at Aug 27, 2025)

Why Every Freshman Needs the Illinois App at UIUC

(updated at Aug 24, 2025)

My First Day at University of Illinois-Urvana Champaign

(created at Aug 22, 2025)

Did you get Selective Service System(SSS) Form 3C?

(updated at Aug 17, 2025)

BLACKPINK's refreshing song - Jump

(updated at Aug 08, 2025)

Poisonous Mushrooms sprouted along the roadside after Typhoon

(updated at Aug 06, 2025)