Vuejs fails to properly transmit data

When I change the image in an image field, the new image data appears correctly before sending it to the back-end. However, after sending the data, the values are empty!

Code

Commented

save_changes() {
    /* eslint-disable */
    if (!this.validateForm) return;
    console.log("image: ", this.data_local.image); // New image data is present
    this.$http
    .put(
        "/api/admin/posts/meta/" + this.$route.params.id,
        this.data_local, // New image data is empty!
        {
        headers: {
            Authorization: localStorage.getItem("access_token"),
        },
        }
    )
    .then((res) => {
    }
    //rest of it...
},
update_avatar(event) {
    this.dialogVisible = true;
    this.dialogImageUrl = URL.createObjectURL(event.target.files[0]);
    this.data_local.image = event.target.files[0]; // Selected image is added to `data_local` object array
},

Screenshots

https://i.stack.imgur.com/NBtic.png

https://i.stack.imgur.com/RceJv.png

Any suggestions?

Update

Header request

https://i.stack.imgur.com/kafos.png

Answer №1

One possible solution is to send it as a FormData object:

let file = this.data_local.image
let image = URL.createObjectURL(file)
let formData = new FormData();
formData.append("image", image);
formData.append("id", 71);
formData.append(....)
this.$http
.put(
    "/api/admin/posts/meta/" + this.$route.params.id,
     formData, 
    {
    headers: {
        Authorization: localStorage.getItem("access_token"),
    },
    }
)
.then((res) => {
}

The headers in the request should resemble the example provided below:

https://i.stack.imgur.com/tboGj.png

Your image data should be in binary format.

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

VueJS throwing an error due to failed type check on prop

Using a component called CardRenderer.vue to render card using an array of Objects has been quite challenging for me. Repeatedly, I have used the same component to render the data and encountered this error: "[Vue warn]: Invalid prop: type check failed for ...

What are the consequences of altering the DOM in React?

As a beginner react developer, I am currently working on creating a MERN App. I have a question for the community regarding my project where I needed to make several changes to the DOM as illustrated below: document.getElementsByTagName('body')[ ...

Unlocking the full potential of Vue-FontAwesome: A step-by-step guide to importing both Free and Pro Icons without

I have possession of a FontAwesome Pro License and I am utilizing the Vue-FontAwesome Component. Whenever attempting to import icons from both the free and Pro repositories, an "Duplicate declaration error ..." occurs. Changing the declaration name result ...

What steps can be taken to display the English translation if the desired translation is not found in the designated language file?

My setup includes two different locales: ru and en. If a translation is not found in the selected language file, how can I display the English version? Here are my current internationalization settings: i18n: { defaultLocale: 'en', f ...

Adjust font size using jQuery to its maximum and minimum limits

My jQuery script enables me to adjust the font-size and line-height of my website's CSS. However, I want to restrict the increase size to three clicks and allow the decrease size only after the increase size link has been clicked - ensuring that the d ...

Ways to transfer a value from a JavaScript file to a PHP file?

Is there a way to transfer data from a JavaScript file to a PHP file? Code: var year = (year != null) ? year : '".$this->arrToday["year"]."'; var month = (month != null) ? month : '".$this->ConvertToDecimal($this>arrTod ...

Web browser local storage

Looking to store the value of an input text box in local storage when a button is submitted <html> <body action="Servlet.do" > <input type="text" name="a"/> <button type="submit"></button> </body> </html> Any sug ...

Results from Flask queries in Vue.js are displaying properly on PC Chrome but not on Android Chrome, Opera, or Edge. However, the results are visible on

I am currently developing a Vue.js website where I am attempting to send a query from the front-end to my Flask backend. The goal is to search through multiple docx documents and display text and images as search results on the screen. Interestingly, my co ...

Refreshing a webpage to accurately display changes made in a CRUD application without the need for a hard reset

My to-do app is almost fully functional - I can create items, mark them as completed, and delete them using a delete button. However, there's one issue: when I delete an item, the page doesn't update in real-time. I have to manually refresh the p ...

Vue-ctk-date-time-picker: Your Ultimate Date and Time Selection Tool for Vue

After spending the last 6 hours trying to solve this issue, I am reaching out for some assistance. I have been using the DateTime picker vue-ctk-date-time-picker, which can be found on GitHub at https://github.com/chronotruck/vue-ctk-date-time-picker In ...

Controller encounters a error when requiring a module

Struggling to set up Stripe for my app, I've encountered some issues with the module implementation. Typically, I would require a module at the top of the file to use it. However, in the paymentCtrl file, when I do this, it doesn't work and I rec ...

Creating reactivity in Vue 3 prop objects: A step-by-step guide

I recently encountered this code snippet in a VueMastery tutorial, but it seems to be outdated: export default { setup(props, {emit}){ let email = props.email; let toggleRead = () => { email.read = !email.read axios.put(`http://loc ...

The Firebase signInWithPopup functionality suddenly shuts down in a Next.js project

Incorporating the signInWithPopup function for signing in has been successful during the development stage on my local server. const firebaseAuth = getAuth(app); const provider = new GoogleAuthProvider(); const [{ user, cartShow, cartItems }, dispatc ...

Unexpected behavior observed with negated character: ? ^

I am looking to create a form where users can input their phone number and have the flexibility to choose how they want to separate the numbers. I have been using a regex pattern for validation: var regex = /[^0-9 \/-\\\(\)\+ ...

Send a PHP array back to jQuery AJAX and dynamically populate a list using conditional statements

Looking to create a dynamic contact list that can be sorted using AJAX. When selecting one of the 3 menu options, a new list with updated information should be displayed. I've been doing some research but haven't come across useful information o ...

Ways to obtain data in JavaScript function from HTML

Struggling to pass the key from a database query in HTML using PHP to a JavaScript function? You're not alone. The challenge lies in passing the field_id to the updateRecords() JS function through the onClick() event of an HTML button. Previously, se ...

What is the solution for - Uncaught TypeError: Cannot access the property 'scrollHeight' of an undefined element?

Could someone kindly assist me in resolving an issue? I recently encountered a problem with a script I have been using on the Chrome console. This script has been working perfectly fine for the past few days. However, today when I ran it, I started receiv ...

Error: The function is not defined on this.props during the handleCHange event

After going through numerous answers to similar questions on this topic, I believe that I am following all the necessary steps but for some reason, it is not working. Below is the specific section of code that is causing the error: import React from &apos ...

How to use the post method in Axios and Spring framework

Currently, I am a newcomer to vue.js and I am on the lookout for methods to effectively store form data using axios into a database. The technologies involved in my project are spring framework along with Thymeleaf template engine. Below is a snippet of m ...

Encountering a parsing issue: an unexpected token was found, expecting "," instead

I'm encountering a Parsing error that states: Unexpected token, expected ",". I've been unable to pinpoint where the missing "," is located. Please refer to the image below for the exact line of the error. const cartSlice = createSlice({ name ...