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

JK960 
Created at Mar 14, 2024 16:07:05 
71   0   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">.


Tags: $.ajax() jQuery Share on Facebook Share on X

◀ PREVIOUS
Difference between Java and Javascript
  Comments 0
Login for comment
SIMILAR POSTS

Launching My First GitHub Repository for WE Service (created at Feb 12, 2024)


OTHER POSTS IN THE SAME CATEGORY

Difference between Java and Javascript (updated at May 09, 2024)

Regular Expressions in JavaScript (created at Feb 12, 2024)

UPDATES

Mordern web design based on Bootstrap (updated at May 09, 2024)

GPL aims to protect the four freedoms of free software (updated at May 09, 2024)

My Clothes - Wide Green Pants (updated at May 09, 2024)

Difference between Java and Javascript (updated at May 09, 2024)

The Timeless Elegance of Men's Balmoral Shoes (updated at May 09, 2024)

The Stylish Charm of Buck Shoes in European Fashion (updated at May 09, 2024)

Unveiling Derby Shoes: A Timeless Classic (updated at May 09, 2024)

Taekwondo Tornado Kick Tutorial (updated at May 09, 2024)

My journey as a Taekwondo assistant master (updated at May 09, 2024)

Taegeuk Chapter 1 (Taegeuk 1Jang) (updated at May 09, 2024)

A Boxed Shoe Cabinet from Costco (updated at May 09, 2024)

Artesia Alley Cleanup: A Day of Community and Contribution (updated at May 09, 2024)

Mom's Amazing Cooking: My Stress Reliever (updated at May 09, 2024)

Kicking Stress Away: From AP Tests to Taekwondo Mats (updated at May 09, 2024)

Python Sets (updated at May 09, 2024)

Python Tuples (updated at May 09, 2024)

Python Conditions and If statements (updated at May 09, 2024)

Python Dictionaries (updated at May 09, 2024)

Python While Loops/For Loops (updated at May 09, 2024)

Python Functions (updated at May 09, 2024)

Python Arrays (updated at May 09, 2024)

Python Lambda (updated at May 09, 2024)

Python Classes/Objects (updated at May 09, 2024)

Python Inheritance (updated at May 09, 2024)

Python Iterators (updated at May 09, 2024)

Python Modules (updated at May 09, 2024)

Python Polymorphism (updated at May 09, 2024)

Python Scope (updated at May 09, 2024)

ILLIT's "Magnetic" in Light of the NewJeans Crisis (updated at May 09, 2024)

AP Exams Calculator Policy (updated at May 09, 2024)

What to Bring on AP Exam Day (created at May 09, 2024)

Exploring UC Irvine (aka UCI) - School and its Majors (updated at May 09, 2024)

Exploring UC Santa Barbara (aka UCSB) - Schools and Majors (updated at May 05, 2024)

Exploring UC Riverside (aka UCR) - Schools and Majors (updated at May 05, 2024)

Exploring UC Santa Cruz (aka UCSC) - Schools and Majors (updated at May 05, 2024)

Exploring UC Davis (aka UCD) - Schools and Majors (updated at May 05, 2024)

Exploring UC Merced (aka UCM) - Schools and Majors (updated at May 05, 2024)

Exploring UC Los Angeles (aka UCLA) - School and its Majors (updated at May 05, 2024)

Exploring UC Berkerly - School and its Majors (updated at May 04, 2024)

Exploring UC San Diego - School and its Majors (updated at May 04, 2024)

Meet NewJeans Danielle: The Rising Star with Killer Dance Moves! (updated at May 04, 2024)

IU's Concerts: A Thrilling Adventure! (updated at May 03, 2024)

Debuting on Billboard Hot 100 Just 8 Days In - ILLIT Rocks the Charts! (updated at May 02, 2024)

The difference between 403 and 404 in HTTP (created at May 02, 2024)

Why DNS Blocking of Illegal Sites is Essential for a Safer Internet (updated at May 01, 2024)

NewJeans's "Bubble Gum" Music Video Sparks Controversy Amidst the Hive-Minheejin Truth Forum (created at Apr 30, 2024)

How do Grass and Trees Grow? (updated at Apr 28, 2024)

Mastering Excel Data Manipulation with Python (updated at Apr 26, 2024)

Why Taekwondo is My Ultimate Stress Buster (created at Apr 23, 2024)

Slice of Heaven: Dining Delights at California Pizza Kitchen, Irvine Spectrum (updated at Apr 23, 2024)