如何在vue.js中将对象附加到formData

时间:2019-10-13 07:30:55

标签: javascript object vue.js axios form-data

我无法将对象追加到FormData

我使用Axios

我不想通过JSON.stringify()发送数据

data () {
   return {
      product: {

          title: '',

          description: '',

          properties: {
             property1: '',
             property2: ''
          }
      }
   }
}

我想要这个

{title: '', description: '', properties:{property1: '', property2: ''}}

2 个答案:

答案 0 :(得分:0)

不幸的是,现在有一种方法可以通过网络发送复杂的数据。不愿意对数据进行分类的原因是什么?

答案 1 :(得分:0)

尝试使用Object.entries。例如...

// If this is the object you want to convert to FormData...
const item = {
    description: 'First item',
    price: 13,
    photo: File
};

const formData = new FormData();

Object.entries(item).forEach(([key, value]) => {
    formData.append(key, value);
});

// At this point, you can then pass formData as the payload to axios

在此处详细了解有关Object.entries()的信息-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries