繁体   English   中英

FormData 使用 axios 发送空数据

[英]FormData sends empty data using axios

我想将我的数据从 vue 发送到 api。 我不知道为什么我的 formData 是空的。 您可以看到附加的快照。 我已经尝试了几个小时使用 stackoverflow this linkthis link上的几个链接

在此处输入图片说明

我的确认方法:

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'
        }
      })
    }

我的代码有什么问题?

data = {'file': this.imagefile , 'data': params}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM