Instead of retrieving a url in an AJAX call, consider fetching an image instead

Here is a snippet from my HTML page. I am looking to replace the image address (result[i].url) with an actual image. I am not sure if this is possible due to the CSS styles being used in this section.

function BindStudents() {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "http://127.0.0.1:8000/memes", success: function (result) {
                   
                    var totalCount = result.length;
                    var structureDiv = "";

                    for (let i = 0; i < totalCount; i++) {
                        structureDiv += "<tr>" +
                            "     <td>" + result[i].id + "</td>" +
                            "      <td>" + result[i].name + "</td>" +
                            "             <td>" + result[i].url + "</td>" +
                            "              <td>" +result[i].caption  + "</td>" +
                            "              <td><button class='btn btn-link' onclick='return confirm(\"Are you sure you want to delete this record?\",DeleteRow(" + result[i].id + "))'>Delete</button></td>" +
                            "           </tr>";
                    }
                     
                    $("#divBody").html(structureDiv);
              
                }
            });
        }

Answer №1

In the event that the server transmits a URL, it will indeed do so.

If your intention is to incorporate an image, you have two options:

  • Modify the server-side code in order to transmit an image (even though JSON does not support image, file, or blob types, you can encode it as text and subsequently decode it on the client side before figuring out how to display it - likely necessitating conversion into a data: URL)
  • Utilize an Ajax request to retrieve the image from the provided URL

A simpler approach would involve creating an <img> element with its src attribute set to the URL sourced from the JSON during the initial HTML creation process.

Answer №2

To insert an image into the HTML, simply use the <img> tag:

structureDiv += "<tr>" +
    "     <td>" + result[i].id + "</td>" +
    "      <td>" + result[i].name + "</td>" +
    "             <td><img src=\"" + result[i].url + "\"></td>" +
    "              <td>" +result[i].caption  + "</td>" +
    "              <td><button class='btn btn-link' onclick='return confirm(\"Are you sure you want to delete this record?\",DeleteRow(" + result[i].id + "))'>Delete</button></td>" +
    "           </tr>";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing the disabled style for an ASP.net dropdownlist

Imagine I have an ASP.net dropdownlist set up like this: <asp:DropDownList ID="ddOwnershipDocumentType" class="msbdd" runat="server"> </asp:DropDownList> Let's take a look at the style definition for msdbdd: .msbdd { font-family: WM ...

Keep the multiselect dropdown list of the select component open

I am currently utilizing the Select component from material-ui-next. Overall, it functions quite smoothly. One scenario where I implement it is within a cell element of an Ag-Grid. Specifically, in this use case, I am making use of the multiselect feature ...

Changing the names of the remaining variables while object destructuring in TypeScript

UPDATE: I have created an issue regarding this topic on github: https://github.com/Microsoft/TypeScript/issues/21265 It appears that the syntax { ...other: xother } is not valid in JavaScript or TypeScript, and should not compile. Initial Query: C ...

What steps can be taken to address the issue of "Failed to compile"?

import React, { useEffect } from 'react' import { Row, Col, List } from 'antd' import Axios from 'axios' import { useState } from 'react' import SideVideo from './Sections/SideVideo' import Subscribe from & ...

Babel failing to transpile source code of npm link/symlink package

I am in the process of establishing a shared module environment using node. Below is an outline of my directory structure: project |--common | |--package.json | |--graphql | |----schema.js | |--server |--package.json |--serv ...

Categorize JQuery Selectors based on the type of elements they

Is there a way to automatically group results by type when using the .each function in JQuery? Consider the example below: $(selector).each(function () { //do stuff here for each found }); The code above goes through each element individually, but ...

Creating a Loopback API that seamlessly integrates with Ember.js

Exploring the use of Loopback to create an API that communicates with Ember. Ember expects JSON data to be enclosed in 'keys', such as for an account: { account: { domain: 'domain.com', subdomain: 'test', title: ...

Is it possible to incorporate spread syntax(...) within a vue.js 2.x template?

Is it possible to utilize a similar structure in a vue.js template? <template> <vxe-table> <vxe-column v-for="options in someConsts" ...options width="160"> </vxe-column> </vxe-ta ...

What is the best way to horizontal center a flexbox within the browser viewport?

Looking to center a flexibox in the browser window? The code in the following jsfiddle works fine, but it doesn't center the flexibox. When the browser screen changes, the image and text adjust responsively. Check out the Fiddle here <!DOCTYPE ht ...

Sticky Sidebar Attached to Parent Container with Smooth Sliding Effect

Is it possible to have my sidebar push the content across when opened, while keeping the fixed navigation relative to its parent element instead of the viewport? The sidebar works fine in Firefox but breaks out of the parent in Chrome. Link to JSFiddle & ...

Grouping object properties in a new array using Java Script

I'm currently learning Java script and attempting to merge an Array of objects based on certain properties of those objects. For instance, I have the following array which consists of objects with properties a, b, c, pet, and age. I aim to create a n ...

Dealing with preventing form submission in JQuery due to the use of Char(13) causing newline issues

Currently, I am facing an issue with capturing the class for my address field. I have successfully prevented the enter key (char[13]) from submitting the form. However, I now need to make an exception for text blocks, such as the address field, where I wan ...

Tips for loading JSON data from a server into a jQuery Mobile list view

Seeking guidance on incorporating JSON data retrieved from a server into a list view while working with jQuery Mobile and PhoneGap. ...

How can the .pre() middleware function in Mongoose be utilized?

I'm curious about the use cases for mongoose .pre('validate') and .pre('save'). I understand their functionality, but I'm struggling to think of specific scenarios where I would need to utilize them. Can't all necessary a ...

The useCallback function is not producing the desired outcome in my code

Upon reviewing the code, it is expected that the useCallback hook triggers the display of the message 'rerendering has happened!' only when typing in the input field, and not when clicking on the Toggle button. However, the message does appear fo ...

Utilizing CodeIgniter: Integrating Retrieved Data into PHP Code Using Ajax Requests

On my website built with Codeigniter, there is a side panel. When I click or select any options in the side panel, the relevant content appears on the main panel. The home page URL is http://localhost/main/userinfo When I click on female, the URL is http ...

Display the uploaded images from uploadify on the webpage

I am currently working on a PHP website that utilizes uploadify for users to upload portfolio images. While I have successfully implemented uploadify, I am now exploring the most effective way to display these uploaded images on the webpage without requir ...

Changing route parameters or query parameters does not trigger a reload of component data in React

The "home" component has links that direct to the product component when clicked. Another component always visible displays links to recently visited products. However, on a product page, these links fail to work properly. Although the URL updates and tri ...

Error during the production build of Next.js Internationalized Routing: Prerendering page encountered an unexpected issue

The configuration in my next.config.js file looks like this: module.exports = withBundleAnalyzer({ i18n: { locales: ['en', 'es'], defaultLocale: 'en', localeDetection: false, }, ... }); This setup allows for ...

Unlock the secret: Using Javascript and Protractor to uncover the elusive "hidden" style attribute

My website has a search feature that displays a warning message when invalid data, such as special characters, is used in the search. Upon loading the page, the CSS initially loads like this: <div class="searchError" id="isearchError" style="display: ...