Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Fetching Wikipedia articles with jQuery AJAX

Posted on April 2, 2025April 2, 2025 by Zubair Shaik

Loading

Fetching Wikipedia Articles with jQuery AJAX: A Comprehensive Guide

Wikipedia is one of the largest repositories of publicly available knowledge. Using its MediaWiki API, developers can fetch and display Wikipedia articles dynamically on a web page. This guide will walk you through how to use jQuery and AJAX to fetch and display Wikipedia articles dynamically.


πŸ“Œ What You’ll Learn

βœ” Understanding the Wikipedia API
βœ” Setting up jQuery in your project
βœ” Using AJAX to fetch Wikipedia data
βœ” Handling API responses
βœ” Displaying Wikipedia search results dynamically
βœ” Enhancing the user experience
βœ” Implementing error handling and security best practices


1️⃣ Understanding the Wikipedia API

Wikipedia provides a public API that allows developers to fetch articles, search Wikipedia, and get metadata about articles.

πŸ“Œ Wikipedia API Endpoint

The Wikipedia API endpoint for searching articles is:

https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch={SEARCH_TERM}

πŸ”Ή action=query β†’ Indicates a search query request
πŸ”Ή format=json β†’ Returns data in JSON format
πŸ”Ή list=search β†’ Performs a search
πŸ”Ή srsearch={SEARCH_TERM} β†’ The search term entered by the user

πŸ‘‰ Example search query for β€œNASA”:

https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=NASA

Wikipedia API Restrictions:

  • Wikipedia does not support CORS (Cross-Origin Resource Sharing) by default, so we need to use JSONP (JSON with Padding) to bypass CORS issues.

2️⃣ Setting Up Your Project

Required Tools

βœ… A text editor (VS Code, Sublime, Notepad++, etc.)
βœ… A web browser (Chrome, Firefox, Edge, etc.)
βœ… Basic knowledge of HTML, CSS, and JavaScript

Adding jQuery to Your Project

You can add jQuery in two ways:

βœ” Option 1: Using a CDN (Recommended)

Add this line inside your <head> tag:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

βœ” Option 2: Downloading jQuery Locally

  1. Download jQuery from https://jquery.com/download/.
  2. Save it in your project folder.
  3. Link it in your HTML file:
<script src="jquery-3.6.0.min.js"></script>

3️⃣ Writing jQuery Code to Fetch Wikipedia Articles

Step 1: Basic AJAX Request to Wikipedia API

$(document).ready(function() {
    var searchQuery = "NASA"; // Change this to any topic

    $.ajax({
        url: "https://en.wikipedia.org/w/api.php",
        data: {
            action: "query",
            format: "json",
            list: "search",
            srsearch: searchQuery
        },
        dataType: "jsonp", // Wikipedia does not support CORS, so we use JSONP
        success: function(response) {
            console.log(response);
        },
        error: function(error) {
            console.log("Error fetching Wikipedia articles:", error);
        }
    });
});

βœ… Explanation:

  • url β†’ The Wikipedia API endpoint.
  • data β†’ Parameters including action, format, list, and srsearch.
  • dataType: "jsonp" β†’ Used to handle CORS issues with Wikipedia API.
  • success(response) β†’ Displays the API response.
  • error(error) β†’ Handles errors if the request fails.

4️⃣ Handling and Displaying Wikipedia API Response

πŸ“Œ Example API Response (Shortened)

{
    "query": {
        "search": [
            {
                "title": "NASA",
                "snippet": "NASA is the United States government agency responsible for space exploration.",
                "pageid": 123456
            },
            {
                "title": "NASA missions",
                "snippet": "NASA has conducted numerous space missions since its founding in 1958.",
                "pageid": 789012
            }
        ]
    }
}

πŸ“Œ Displaying Wikipedia Search Results in HTML

Modify the script to display search results dynamically:

$(document).ready(function() {
    $("#search-btn").click(function() {
        var searchQuery = $("#search-input").val(); // Get user input

        $.ajax({
            url: "https://en.wikipedia.org/w/api.php",
            data: {
                action: "query",
                format: "json",
                list: "search",
                srsearch: searchQuery
            },
            dataType: "jsonp",
            success: function(response) {
                $("#results").empty(); // Clear previous results
                
                $.each(response.query.search, function(index, article) {
                    var articleTitle = article.title;
                    var articleSnippet = article.snippet;
                    var articleURL = "https://en.wikipedia.org/wiki/" + articleTitle.replace(/ /g, "_");

                    var resultHTML = `
                        <div class="article">
                            <h3><a href="${articleURL}" target="_blank">${articleTitle}</a></h3>
                            <p>${articleSnippet}...</p>
                        </div>
                    `;

                    $("#results").append(resultHTML);
                });
            },
            error: function() {
                $("#results").html("<p>Failed to fetch Wikipedia articles. Please try again.</p>");
            }
        });
    });
});

πŸ“Œ Updating the HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Wikipedia Search</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>Wikipedia Article Search</h1>
    <input type="text" id="search-input" placeholder="Enter search term">
    <button id="search-btn">Search</button>
    <div id="results"></div>
</body>
</html>

βœ… How It Works:

  • User enters a search term and clicks the “Search” button.
  • AJAX request fetches Wikipedia search results.
  • Results are displayed dynamically with title, snippet, and a clickable link.

5️⃣ Enhancing User Experience

To improve the user experience, let’s add:
βœ… Loading Indicator
βœ… Error Handling

Modify the JavaScript:

$("#search-btn").click(function() {
    var searchQuery = $("#search-input").val();
    
    $("#results").html("<p>Loading...</p>"); // Show loading message

    $.ajax({
        url: "https://en.wikipedia.org/w/api.php",
        data: {
            action: "query",
            format: "json",
            list: "search",
            srsearch: searchQuery
        },
        dataType: "jsonp",
        success: function(response) {
            $("#results").empty();
            
            if (response.query.search.length === 0) {
                $("#results").html("<p>No articles found.</p>");
                return;
            }

            $.each(response.query.search, function(index, article) {
                var articleTitle = article.title;
                var articleSnippet = article.snippet;
                var articleURL = "https://en.wikipedia.org/wiki/" + articleTitle.replace(/ /g, "_");

                var resultHTML = `
                    <div class="article">
                        <h3><a href="${articleURL}" target="_blank">${articleTitle}</a></h3>
                        <p>${articleSnippet}...</p>
                    </div>
                `;

                $("#results").append(resultHTML);
            });
        },
        error: function() {
            $("#results").html("<p>Error fetching data. Try again later.</p>");
        }
    });
});

Using jQuery and AJAX, we successfully built a Wikipedia search tool that fetches articles dynamically.

Would you like to add animations or autocomplete suggestions? Let me know!

Posted Under jQueryAJAX AJAX requests API Error Handling API fetching API requests Dynamic Content Fetch Wikipedia Data Fetching Wikipedia Articles frontend development JavaScript JavaScript fetch API JavaScript Libraries jQuery JSON Data Live Data Fetching Open API Open Source APIs REST API Web API Web Development Wikipedia AJAX Wikipedia API Wikipedia API Authentication Wikipedia API Best Practices Wikipedia API Call Wikipedia API CORS Issues Wikipedia API Documentation Wikipedia API Endpoint Wikipedia API Example Wikipedia API Integration Wikipedia API Integration Guide Wikipedia API JavaScript Wikipedia API jQuery Wikipedia API Key Wikipedia API Performance Optimization Wikipedia API Response Handling. Wikipedia API Tutorial Wikipedia API URL Wikipedia Article Display Wikipedia Article Fetching Wikipedia Article Metadata Wikipedia Autocomplete Wikipedia Data Wikipedia Data Extraction Wikipedia Data Fetching Wikipedia Data Visualization Wikipedia JSONP Wikipedia Live Search Wikipedia Open Source Wikipedia Page API Wikipedia Query API Wikipedia Query Search Wikipedia Search Wikipedia Search API Wikipedia Search Bar Wikipedia Search Engine Wikipedia Search Results Wikipedia Search Tool Wikipedia Snippets

Post navigation

Fetching real-time weather updates using jQuery
Conflicting multiple default exports in the same file

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
Β© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions