N
N
Nightmare0582021-02-04 05:31:21
Vue.js
Nightmare058, 2021-02-04 05:31:21

Is there an alternative to submit via FormData?

Good day, dear ones.
The crux of the matter is I need to send form data + image via axios.
Google suggested a variant with FormData, where an empty object is created and field values ​​​​are attached to it through append.
Because of this, I end up with rather unwieldy code.
Maybe there are some other ways to not do appen for each field?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2021-02-04
@Nightmare058

It is only desirable that your form was made according to the rules. The elements had all the required attributes, such as name and value. An alternative option is to have an object in the vue property for each field of the form or for the entire form - data(){} And when the values ​​in the inputs change, update the data in this object. And when submitting, send this object in json format, for example. Only the picture will need to be converted, say, to base64 or to something that your backend can process.
const data = new FormData(formElement)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question