methods for extracting json data from the dom with the help of vue js

Can anyone help me with accessing JSON data in the DOM using Vue.js?

Here is my script tag:

<script>
import axios from "axios";
export default {
  components: {},
  data() {
    return {
      responseObject: ""
    };
  },
  async mounted() {
    try {
      const response = await axios.get("http://localhost:4000/api/blogs");
      console.log(response);
    } catch (e) {
    }
  }
};
</script>

My template to display the data is not showing anything:

  <div>
    {{responseObject}}
  </div>

This is the JSON data I want to display:

{
"status": true,
"responseObject": {
        "resource": "services",
        "status": "success",
        "data": [
            {
                "service_id": "0001",
                "service_name": "Buy Airtime",
                "service_type": "airtime"
            },
]}}

Could someone please guide me on how to display this JSON data in the DOM?

Answer №1

To achieve this, you can easily assign the response to a property in your data object.

Check out the live demonstration below with descriptive comments added:

new Vue({
  el: '#app',
  data: {
    responseObject: ''
  },
  mounted() {
    // For demonstration purposes, a mock response is used here. You can replace it with an actual API call.
    const response = {
      "status": true,
      "responseObject": {
        "resource": "services",
        "status": "success",
        "data": [{
          "service_id": "0001",
          "service_name": "Buy Airtime",
          "service_type": "airtime"
        }]
      }
    };
    
    // Assign the necessary object from the response to the 'responseObject' data property.
    this.responseObject = response.responseObject.data;
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="service in responseObject" :key="service.service_id">
    <h2>{{ service.service_name }}</h2>
    <p>{{ service.service_type }}</p>
  </div>
</div>

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

Conceal a table with jQuery

Is there a way to initially conceal a table on page load only to reveal it with a sliding animation when a specific link is clicked? I have attempted using the following code to hide the table: $('.table_div').hide(); I have enclosed the table ...

Nested arrays in Vue.js

I have encountered an issue with a nested array in my data set while looping through the parent elements in my code. The main problem arises when trying to display the nested array. Below are the relevant parts of my code: As I iterate through my columns ...

Ruby on Rails slider bar functionality

Currently, I am developing a video-focused application using Ruby 1.9.2 and Rails 3.1. One of the key features I need to implement is a slider bar that allows users to adjust the total duration of a video in real-time. Despite my attempts to use HTML5 fo ...

Implementing the adding functionality within an ejs file

I'm currently working on a project that involves fetching data from an API using a simple JavaScript file upon clicking a button. Initially, everything was functioning properly when both the HTML file and JS file were in the same folder, and I could a ...

"The use of objects as a React child is not permitted" and code linters

I'm encountering an issue with a simple component and I can't figure out why. The error message and code snippet are as follows: Error: Objects are not valid as a React child (found: object with keys {Cfor, children}). If you meant to render a ...

Is it possible to make a call to an endpoint from within the node project?

I'm currently working on a MERN app that dynamically adds meta tags to React pages without using server-side rendering. In order to achieve this, I need to extract the query parameters from the main server file and assign the relevant metadata content ...

Animate the React Bootstrap modal only when it is shown or hidden

I am experimenting with the idea of transitioning smoothly from one modal to another in bootstrap Modals for react (using react-bootstrap). However, I am facing challenges due to the fade CSS class causing flickers and unwanted animations. My goal is to h ...

What is the solution to the error message that states a bind message provides 4 parameters, while a prepared statement "" necessitates 5?

Is there a way to fix the issue where the bind message provides 4 parameters but the prepared statement "" requires 5? I've tried solutions from others who faced similar problems without success. (I've included all classes for better error unders ...

JavaScript not functional, database being updated through AJAX calls

I have created a game using Phaser, a JavaScript library for games. Now I am looking to implement a score table using JS/PHP. My main focus is on transferring a variable from JS to PHP in order to update the database. I have researched numerous topics and ...

What are some creative ways to provide video responses to a list of questions?

My task is to create a popup that includes questions and video responses. The challenging aspect for me is how to ensure each question displays a different video in the same location. Additionally, when the user first opens the popup, the initial question ...

Express server is receiving undefined post parameters from Axios in Vue, even though they are clearly defined in Vue

Within my code, I am utilizing an <img> element as shown below: <img v-bind:word = "thing" @click="action" align="center" src="../assets/pic.png"/> Alongside, there is a method structured in this manner: ...

Opening a Bootstrap Modal in React without relying on npm react-bootstrap

I've been trying to create a Modal in React.js using Bootstrap5, but I'm unable to use npm react-bootstrap for various reasons. I attempted an approach where I utilized state to set Modal classes with a button, which worked well with my NavBar, b ...

Leverage the power of Vuejs to create a seamless user experience with one tab dedicated to control

Thinking about developing an App using vuejs for showcasing photos, I am interested in segregating the view container from the photo control by either a new tab or a separate browser instance. How should I kickstart a project like this? Should I opt for tw ...

mention a Vue.js component computed property that points to a specific DOM element

Is there a way to access the DOM element that initiated a method inside a Vue component computed property? For example: <template> <img :src="getUrl" class="image1"/> <img :src="getUrl" class="image2"/> </template> <scri ...

The malfunctioning of my Jquery datepicker

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <script src="jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).r ...

Implementing Mocks for window.eventBus.$on in Vue.js using Jest Framework

Looking to verify the emitted value for test case coverage. window.eventBus.$on('filter-search-content', () => { console.log('The event was emitted successfully'); this.showFilter = true; }); I attempted the following approach, ...

Using Vue's v-tabs as a template component does not display the v-item content

Looking to create a customizable tab component using v-tab and slots in Vue (in TS). The current tab number is passed as a property from the URL. However, I'm encountering an issue where the content of v-tab-items is not rendering, only the bar with n ...

The Mystery of the Undefined Return Value in JavaScript's Map Function

My array contains around 1000 elements. I'm encountering a situation where the map function returns undefined for certain indexes. Is there a method to only retrieve values that meet a specific condition? I want to filter out elements with values less ...

What is the integration process of using Font Awesome with React?

After installing react-create-app using npm, I also added react-fontawesome. Now, I'm wondering how to include the css styles of fontawesome in my project? Here is a glimpse of my work space: https://i.stack.imgur.com/pM1g1.png ...

JavaScript does not show an error message if a function is called but has not been defined

Currently, I am developing a nodejs/reactjs application that incorporates a cache system. During my development process, I encountered an interesting error where the data stored in the cache was not being displayed by the component. After thorough investig ...