Using an image tag with dual quotes in Vue.js

I am currently working on a feature where users can upload an image and then have it displayed after the upload is complete. However, I am facing an issue where the response link of the image contains a double quote, and when I use the <img> tag to display the image, it adds another set of double quotes which prevents the image from being displayed. I tried using .slice(1,-1) but it did not solve the problem. How can I correctly display the image with just a single set of double quotes in the <img> tag?

<template>
      <img :src="srcUrl"/> 
</template>

<script>
uploadFile(file) {
      const formData = new FormData();
      formData.append("file", file);
      const request = new XMLHttpRequest();

      request.addEventListener("readystatechange", () => {
        if (request.readyState === 4) {
          const uploadedUrl = request.response;
          this.srcUrl = uploadedUrl; //getting a link with double quotes here and used slice but didn't work
        }
      });
      request.open("POST", "BaseURI");
      request.send(formData);
    },
</script>

Answer №1

Update: attempting to retrieve data from an API led us to discover that it was actually a graphql API. Here is a breakdown of the process.

This is how a graphql playground appears. Input your parameters on the left (query) and view the results on the right, under data. https://i.sstatic.net/EQU3Y.png

Shown here is my photo query; I have another one on a different tab.
Photos query below:

query {
  photos {
    data {
      id
    }
  }
}

Essentially, these are similar to REST GET requests, but with a more intricate structure and backend organization.

I've created a codesandbox example to visually demonstrate how it operates. Access it here: https://codesandbox.io/s/somehow-working-graphql-example-3rqvj?file=/src/components/Photos.vue

The gql endpoint can be found in main.js or at . You can experiment freely by writing queries on the left and pressing the "play" button!
There is also a helpful docs section on the right for guidance. https://i.sstatic.net/8xCck.png

The linked project contains all gql calls in /graphql/ folder, with Photos.vue showcasing one method of executing the call (multiple options available). The setup currently supports one ApolloQuery, so if you want to see results, try commenting out one and viewing the other. If both are uncommented simultaneously, it may cause issues due to configuration complexities - easier to clone and test locally instead.
You might need to refresh or restart the sandbox due to dynamic import complications... Alternatively, consider cloning and trying locally for smoother testing.


In conclusion, this demonstrates how to fetch data from a graphql API and display it on your website.
I highly recommend checking out this concise free course on YouTube to enhance your understanding: https://www.youtube.com/watch?v=ygUDIeiYZNA&list=PLTRTpHrUcSB--g_8qkmycKyB0WAua9sZR

https://i.sstatic.net/Zjsxi.png

Hoping this information proves useful in some way; unable to provide further assistance presently.

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

Ways to verify if element has been loaded through AJAX request

I am trying to determine if an element has already been loaded. HTML <button>load</button> JS $(document).on('click','button',function () { $.ajax({ url: 'additional.html', context: document ...

What is the best way to compare dates in JavaScript using this particular format?

Is there a way to compare the current date and time in JavaScript with the specific format of '2016-06-01T00:00:00Z'? I am receiving the date format '2016-06-01T00:00:00Z' from the backend, and I need to verify it against today's ...

Unable to process despite clicking button (no error messages)

I'm in the process of setting up an options page for my very first extension. As a beginner in coding, I might have made some rookie mistakes along the way. However, I am facing challenges with basic functionality that was previously working seamlessl ...

Organize data by multiple criteria using List.js

Attempting to configure the list.js plugin to allow sorting based on multiple values. Specifically, the goal is to sort by category first and then alphabetically by title within each category. See a demo here: http://jsfiddle.net/8E7cH/ This functional ...

Is there a way to exit from await Promise.all once any promise has been fulfilled in Chrome version 80?

Seeking the most efficient way to determine which server will respond to a request, I initially attempted sending requests in sequence. However, desiring to expedite this probing process, I revised my code as follows: async function probing(servers) { ...

What is the best way to eliminate duplicate items in JavaScript?

Note: I am aware that we use Set to eliminate duplicates in an array. I have a date dropdown. When I select a date, it displays the date correctly in a list. However, the problem arises when I select an already chosen date, it still shows up in the list. ...

When utilizing TinyMCE in numerous Vuetify dialogs, the editor appears completely empty

My challenge is using TinyMCE in multiple dialogs, as switching dialogs causes the TinyMCE editor to become blank and uneditable. To showcase the issue I'm facing, I have prepared a demo accessible here: https://codesandbox.io/s/tinymce-vue-demo-fork ...

How to adjust the minimum height of v-application--wrap in Vuetify

The layout of my page includes a vuetify component as a widget, followed by other content. However, there is too much space between the widget and the rest of the page, causing the vuetify app to take up more height than desired. I've attempted to adj ...

Error: Missing authorization header on certain requests for Axios in iOS devices

I have a vuejs application where I am utilizing axios for handling HTTP requests. The authorization header is being set through a request interceptor, like this: const api = axios.create({ baseURL: process.env.API_URL, crossdomain: true, headers: { ...

Avoiding an endless JavaScript loop in Selenium/PhantomJS to improve test automation sanity

Imagine a scenario where a web application contains JavaScript code like the one below: for(i=0; i > -1; i++){var a=1}; // Infinite loop If I attempt to navigate this web app using Selenium or PhantomJS, it will become unresponsive indefinitely. Is t ...

The value returned by a component should remain consistent regardless of where it is invoked. Additionally, the main component in React should not re-render when the state of a sub-component is

I am looking to have the same value displayed in the Home function from the Component, without causing a rerender when the useState in Component is updated. import { useState, useEffect } from "react"; function Component() { const [count, setC ...

Next.js application shows 404 errors when trying to access assets within the public directory

I've been struggling to display the favicon for my site (which uses next.js). Despite going through numerous Stack Overflow posts and tutorials, I'm feeling increasingly frustrated. The structure of my project, particularly the public directory, ...

Converting coordinates to pixel-based fixed positioning in CSS

After creating an animated square pie chart using basic CSS to display it in a grid format, I am now looking to transform the larger squares into a state map grid. Any ideas on how to achieve this? In my JavaScript code snippet below, I believe there is a ...

What causes the oninput event to act differently in Angular as opposed to regular JavaScript?

As I delve into learning Angular with TypeScript, I've encountered some inconsistencies compared to JavaScript that are puzzling me. Take for example this function that works flawlessly with pure JavaScript, where it dynamically sets the min and max a ...

What is the best way to find a partial string match within an array of objects when using Jest?

I am currently utilizing the following versions: Node.js: 9.8.0 Jest: 22.4.2 A function called myFunction is returning an array structured like this: [ ... { id: 00000000, path: "www.someUrl.com/some/path/to" } ... ] I ...

Setting URL parameters in a POST request: A guide

Currently, the data in question is structured as JSON within this code snippet. However, I've received feedback indicating that it should actually be implemented as URL parameters. I'm currently facing some difficulties with modifying this to fit ...

Why does getElementById work when getElementsByClassName doesn't?

I created a script with the purpose of hiding images one and two, while keeping image 3 visible and moving it into their place. The script functions correctly when using div Id's instead of div Classes. However, I prefer to use div classes for groupin ...

I'm curious if it's possible to modify a webpage loaded by HtmlUnit prior to the execution of any javascript code

To begin, I want to elaborate on the reasoning behind my question. My current task involves testing a complex web page using Selenium + HtmlUnit, which triggers various JavaScript scripts. This issue is likely a common one. One specific problem I encount ...

JSDOM failing to retrieve complete list of elements from webpage

I'm currently struggling with a simple webscraper using JSDOM. Here's the code snippet I've been working on: const axios = require("axios"); const jsdom = require("jsdom"); const { JSDOM } = jsdom; let v = "15" ...

Encountered a TypeScript error in Vue 3 when attempting to access state within the setup method: "TS error -

Currently, I am working with Vue 3 and TypeScript. In the Setup function, I have defined some states like this: export default defineComponent({ setup() { const isLoadingSendData = ref(false) return { isLoadingSendData } }, methods: { ...