FormData отправляет пустые данные, используя axios - PullRequest
0 голосов
/ 26 марта 2020

Я хочу отправить свои данные из vue в API. Я не знаю, почему мои formData пустые. Вы можете увидеть прикрепленный снимок. Я пробовал несколько часов, используя несколько ссылок на stackoverflow эту ссылку и эту ссылку .

enter image description here

мой метод подтверждения:

async confirm () {
      let formData = new FormData()
      let params = {
        name: this.product.name,
        department_id: this.departmentId,
        category_id: this.categoryId,
        sub_category_id: this.subCategoryId,
        brand_id: this.brandId,
        weight: parseInt(this.product.weight),
        unit_weight: this.product.unitWeight === 'gram' ? 'g' : 'ml',
        volume: {
          length: parseInt(this.product.volume.length),
          width: parseInt(this.product.volume.width),
          height: parseInt(this.product.volume.height)
        }
      }
      formData.append('file', this.imageFile)
      formData.append('data', JSON.stringify(params))
      console.log(formData.getAll('data'))
      console.log(formData.getAll('file'))
      console.log('this.imagefile', this.imageFile)
      console.log('this.params', params)
      console.log('formData', formData)
      this.fullscreenLoading = true
      await this.klikdailyAPI().post('products', formData).then(result => {
        let message = this.product.name + ' succesfully created'
        this.$store.commit('products/setMessage', message)
        this.$router.push('/products')
        this.fullscreenLoading = false
      }).catch(err => {
        this.snackbar = true
        this.snackbarObject = {
          color: 'error',
          text: 'ERROR! ' + err.response.data.message
        }
      })
    }

мой метод API:

klikdailyAPI () {
      return axios.create({
        baseURL: `${url}/api/v1/`,
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('tokenApi')}`,
          'Content-Type': 'multipart/form-data'
        }
      })
    }

Что не так с моим кодом?

1 Ответ

0 голосов
/ 29 марта 2020
data = {'file': this.imagefile , 'data': params}

axios.post('/api', data, header).then( response => { console.log(response); } )
...