Show image from API using JS/Vue.js version 3

I've been attempting to show an image fetched from an API, but haven't had any luck.

It displays normally in Postman:

However, when I use console.log to view the returned data, it shows as follows:

How can I convert this into a properly formatted string for the src attribute?

Thank you!

Answer №1

Chances are high that when making a fetch request, you might be attempting to parse JSON data instead of parsing it as a blob.

fetch(url, options)
.then(res => res.blob())
.then(res => //perform your operations here)
.catch(error => console.log(error))

Answer №2

When utilizing Axios, it was important for me to adjust the responseType to "blob":

try {
    const response = await api.post(
      url,
      {
        params
      },
      {
        responseType: "blob"
      }
    );

    return URL.createObjectURL(
      new Blob([response.data], { type: "image/png" })
    );
} catch (error) {
    console.error(error);
}

The solution involved simply including the return in the src attribute of the image.

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

What is causing my component to render the count value twice?

This is my main layout component: function MainPage() { return( <div> <MLayout children={<MNavbar custOrFalse={false} />} /> </div> ); } export default MainPage; Here is the child navbar compone ...

What is preventing me from assigning a value of false to my JavaScript variable?

I'm encountering an issue where the articleExists variable is not being set to true on line 6, even though I have used console logs to double check that the if statement containing it is functioning properly. app.post("/articles", function(req, res) ...

Improving the efficiency and readability of JavaScript syntax (specifically conditional statements with if-else) in Vue.js

Seeking advice on optimizing and simplifying this syntax in Vue's methods section: methods: { getColorBtn(status, isCorrect, isRemind, textButton) { if (status === 1 && isCorrect === 1 && isRemind === 1) return 'v-btn--outlined theme--l ...

Button spanning the entire width, specifically designed for extra small screens

I am currently utilizing the Material UI framework to build a form, which includes a <Button> component nested inside a Grid. My objective is to maintain the standard width and height of the button on md screens and above, while ensuring it occupies ...

Angular filter that replaces underscores with spaces

Looking for a solution to replace underscores with spaces in a string ...

Utilize jQuery to cycle through images as the background of a div

Whenever I hover over a div, it successfully cycles and changes the background-image, while stopping the other divs from cycling. The issue arises when each div has a title that appears upon hovering, and if I quickly move my cursor from one title to anot ...

Issue with conditional comment in IE 8 not functioning as expected

Struggling with changing the SRC attribute of my iFrame specifically for users on IE 8 or older. This is the code I have written: <script> var i_am_old_ie = false; <!--[if lte IE 8]> i_am_old_ie = true; <![endif]--> </script> ...

Obtain the AJAX response in separate div elements depending on whether it is successful or an error

Currently, my jQuery script outputs the result in the same div for error or success messages: HTML <div id="error-message").html(res); JQUERY jQuery('#register-me').on('click',function(){ $("#myform").hide(); jQuery ...

Have you ever wondered why the Chart is being added to the current div when employing ng-repeat?

I have successfully created a dynamic bar chart and placed it inside a div. However, when I try to achieve the same result using ng-repeat, the new chart is appended to the existing one. Below is my code: HTML: <div id="main" class="drop-container" ...

The Bootstrap 4 Modal has a one-time activation limit

It seems that I mistakenly created two modals. The issue arises when I open either of them for the first time and then close it, as from that point on, neither modal is able to be opened again by performing the same action that initially worked. https://i ...

"Endless loop error in Three.js causing a system crash

I'm currently in the process of developing a library that consists of 'letter' functions responsible for generating letters in vertex coordinates. The main objective here is to allow users to create words or sentences using an interactive Po ...

Tips for modifying the content of a div within an HTML document after a certain period of time has elapsed, and then repeating the process

Currently, I am in the process of developing an Android app that will function as a Digital Display. One of the challenges I am facing involves displaying an HTML page with multiple regions or boxes, each containing various items that need to switch dynami ...

The precision of the css function in jQuery

Possible Duplicate: jQuery and setting margin using jQuery In the HTML snippet below, I have set margins for a paragraph element to center it: <p style="margin-left:auto; margin-right:auto; width:200px;">Hello</p> After that, I attempted ...

What is the best way to embed Javascript scripts within existing Javascript code on the client side?

I am in the process of developing an innovative HTML5 game engine. My goal is to streamline the inclusion process by having just one file, engine.js, required in the HTML document. This script will establish a global Engine object that will grant users acc ...

The request body contains no information

I'm having trouble debugging this issue. Can anyone help me out? When using console.log(req.body), I am getting an empty object {}. I've tried multiple approaches but still can't figure out the problem. Even after attempting to use middle ...

Unable to add a string to a http get request in Angular

When a specific ID is typed into the input field, it will be saved as searchText: <form class="input-group" ng-submit="getMainData()"> <input type="text" class="form-control" ng-model="searchText" placeholder=" Type KvK-nummer and Press Enter" ...

When using AngularJS, encountered an issue where a view was not updating with new data from a $http request

When making a request to the 500px API using $http for popular photos, the response object is successfully returned. However, I am facing difficulty in pushing the retrieved photo items to the view. Below is my current controller code: meanApp.controller ...

What are some tips for dynamically updating the Toolbar in React Material UI through programming?

One of the challenges I'm facing is with a Material UI Select component that I use to navigate between several pages. While this component is shared across all pages, I have specific Toolbar components on certain pages that I would like to programmati ...

Issue with PrimeFaces radiobutton styles not updating after being clicked programmatically

My setup includes a p:selectOneRadio constructed like this: <p:selectOneRadio id="positionRadio" value="#{employeeBean.empPosition}" converter="#{empPositionConverter}" layout="custom" required="true" requiredMessage="Please select ...

Navigating a collection of objects after a button is clicked

My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop: const addBook = (ev) => { ev.preventDefault(); let myLibrary = []; let bookIn ...