Retrieving HTML content from an API using Vue.js

So, I'm working on retrieving data from an API using vue.js and axios. In my app.vue file, I import a component called Contests where I make the API call. Even though I can retrieve the data, it's rendering as HTML when displayed in my final screen component. Any ideas on how to fix this issue? Below is the code snippet:

<template>
    <div>
        <app-contests> </app-contests>
    </div>
</template>

<script>
    import Contests from './components/Contests.vue';

    export default {
        components: {
            appContests: Contests
        }
    }
</script>

<style>

</style>

This is where I am attempting to fetch the data:

<template>
    <div>
        <div class="container">
            {{info}} 
        </div>
        <div v-if="errored">
            <h1>We're sorry, we cannot retrieve this information at the moment. Please come back later.</h1>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                info: null
            }
        },
        mounted() {
            axios
              .get('myApiThatReturnsHtml')
              .then(response => {
                this.info = response;
              })
              .catch(error => {
                console.log(error);
                this.errored = true
              })
              .finally(() => this.loading = false)  
        }
    }

</script>

<style>

</style>

Answer №1

To properly display the HTML content of your response data on the DOM, you will need to utilize a suitable handler for this task. Vue.js offers the v-html attribute specifically designed for adding HTML content to the DOM.

<div class="container" v-html="info">
</div>

However, caution must be exercised when using this feature as it has the potential to leave your application vulnerable to XSS attacks. Learn more about XSS vulnerabilities in Vue.js at

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

Can you please provide guidance on how to dynamically add number values from an array to a select tag in Vue.js?

I'm trying to populate a select tag with options from an array variable that contains arrays of number values. However, the values being output appear to be blank. Here is the HTML code: <select required id="dropDown"> <option>Sele ...

Using an array in ExpressJS to access JSON data

When I send JSON data via jQuery on a webpage, it appears as follows: $.post('/url', data); The data is a JavaScript object with values and an array. The JSON.stringify(data) output looks like this: {"favoriteAnimal":"piglet", "okayAnimals":[" ...

A step-by-step guide on integrating HTML form input with an API object array

I'm looking to combine two code "snippets" but I'm not sure how to do it. One part of the code turns HTML form input into a JSON object. You can see the CodePen example here. What I want is when the "Add note" button is clicked, I want to grab ...

RootState not able to access Vuex module

In order to preload filter settings and update the application state, I had to access the route's query parameters inside Vuex. To achieve this functionality, I implemented vuex-router-sync from this Github repository. The next step involved synchron ...

incorporating new content into a jQuery list widget

i am currently exploring this demo for niceforms: http://www.emblematiq.com/lab/niceforms/demo/v20/niceforms.html i want to understand how i can integrate a datasource with the existing list on this page: <select size="4" name="languages[]" id="langu ...

How can I navigate to a different page using Node.js and Express?

I am currently developing a chat application in Node.js with the use of socket.io. Whenever a new user logs into localhost:3000, they are presented with a form where they can input their name and the room they wish to join. How can I efficiently redirect t ...

Fulfill a promise based on a particular event in Puppeteer

I am looking for a way to seamlessly continue my puppeteer code after a particular event occurs. Specifically, I need guidance on how to handle the 'request' event in a synchronous manner. Here is an example of the event code: await page.on(&apo ...

Disabling selects that were previously disabled after submitting a form

I am facing a challenge with a form that contains multiple select fields. Some of these select fields are disabled to prevent users from modifying the values. However, for validation purposes, I still need to ensure that even the disabled <select> ...

Inquiry regarding modules in Javascript/Typescript: export/import and declarations of functions/objects

I'm fresh to the realm of TypeScript and modules. I have here a file/module that has got me puzzled, and I could really use some guidance on deciphering it: export default function MyAdapter (client: Pool): AdapterType { return { async foo ( ...

Consolidate the indexes of arrays into a single object

Is there a way to combine array indexes into one object using a key-value pair format, like {authors[i]: quotes[i]} in my example? If you'd like to review the code, please visit my Codepen: http://codepen.io/anon/pen/Ndezeo Thank you. ...

The node.js util module is not compatible with my Vue project

After creating a vue project using vue cli 3 with the command below: vue create hello-world I want to use the util module of Node.js (this one), but when I try to access it in my Vue.js project, it loads Vue's own module instead. How can I load the ...

Enhance Your HTML Skills: Amplifying Table Display with Images

Recently, I utilized HTML to design a table. The Table Facts : In the first row, I included an appealing image. The second row contains several pieces of content. In continuation, I added a third row. The contents in this row are extensive, resulting i ...

Retrieving nested JSON object data with Angular's HTTP GET method

Currently, I am attempting to extract a URL from a JSON object that is nested, but I am encountering difficulties accessing the data. I am unsure of the method to retrieve data from a JSON object that is deeply nested. app.controller('mainCtrl', ...

Is there a way I can appropriately display an image in a specific size box?

Check out the code snippet I wrote below: import React from 'react' function OurCourse() { return ( <div className='w-full '> <div className='w-full h-[390px]' style={{ backgroundImage:&apos ...

What is the best way to replace the empty months in an array with zeros?

I have a dataset with information about customer data for specific months, but some months are missing. The current array looks like this: dashCustomerData = [{ "req_count": 1, "revenue": 11868.19, "getMonth": "August", "count(compID)": 2 ...

Organizing a collection of clothing sizes with a mix of different measurements using JavaScript

If I have an array like this: $arr = ['xl', 's', '1', '10', '3', 'xs', 'm', '3T', 'xxl', 'xxs', 'one size']; I aim to arrange the array in this o ...

Navigating through the endless scroll behavior in Twitter using CasperJS (PhantomJS)

Having trouble with infinite scrolling on Twitter, as the page is not loading new content even when scrolling to the bottom. To test if any content loads at all, I have used the following code snippet: casper.open('https://twitter.com/<account> ...

I'm confused, I installed the module but it's still showing an error message saying it can

I've been working on coding a discord music bot, and here is the code I have so far: const config = require('config.json') const Discord = require('discord.js'); const ffmpeg = require('ffmpeg-extra') const client = new ...

Unable to invoke function within class using 'this'

I've been working on a class named Scheduler that runs a cron job using the cron module. I've defined a function to calculate the difference in days between two dates, and it works fine when called outside of the cron job iteration. However, if I ...

Can a dynamic field name be incorporated into a JavaScript Object?

My goal is to dynamically add a field name and update the value based on that. In my situation, the eventType can have 4 types: delivery, send, open, click. However, when I implement this code, I am only getting the eventType as a string. const event = J ...