0

i am working on a vue / laravel project and i want to send an array and formdata with axios .

this is my code:

submit(){
    //The FormData
    const formData = new FormData
    formData.set('images', this.imagesInfo)

    //The Array
    this.product ={
        data: this.data, 
        option: this.option
    }

    //How can i send *this.product* and *formData* ?
    axios.post('/admin/product/add', ****)
}

How can i send this.product and formData with axios?

1
  • Your "array" isn't an array, it's an object. How do you want to send it, as JSON or application/x-www-form-urlencoded? How do you intend on reading it server-side? Commented Aug 19, 2021 at 5:50

1 Answer 1

3

In form data, you can't send array directly.

In order to send array in formdata you have to run a loop and pass values like this:

const array = [1,2,3,4,5,6];

const formData = new FormData();

array.forEach(function(value) {
  formData.append("id[]", value) // you have to add array symbol after the key name
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.