如何在Vue JS和Node JS中通过Axios发送formData?

时间:2019-02-15 20:33:09

标签: node.js vue.js axios

  

Api.Js->   这是Api(节点Js)的连接器

right click class > Run ClassName.main()
  

PageService.js

import axios from 'axios'

export default () => {
    return axios.create({
        baseURL: "http://localhost:4040",
        credentials: true
        //WithCredential: true
        //WithCredentials : true
    })
}
  

Page.vue

import Api from "@/services/Api";

export default{
    threadSubmit(params) {
        return Api().post("page/threadSubmit", params);
    },
    // threadSubmit(params) {
    //     return Api().post("page/threadSubmit", params, {
    //         headers: {
    //             'Content-Type': 'multipart/form-data'
    //         }
    //     });
    // }
}

问题在于,在每个请求#FormData中发送空结果。 如何使用该Api.js文件发送多部分表单数据? 谢谢你。

1 个答案:

答案 0 :(得分:0)

尝试此代码。

import axios from 'axios'

export default () => {
    return axios.create({
        baseURL: "http://localhost:4040",
        credentials: true,
        headers :{
           'Content-Type': undefined
        }
        //WithCredential: true
        //WithCredentials : true
    })
}


 async feedFinalSubmit(){
            const formData = new FormData();
            formData.append("files", this.file);
            formData.append("description", this.thread.description);
            formData.append("pageID", this.$route.params.id);

            const dict = {};
            dict['pageID']=this.$route.params.id;
            dict['description']=this.thread.description;

            const blob = new Blob([dict], {
                        type: 'application/json'
                        });
            formData.append("JsonData", blob); 
            const response = await PageService.threadSubmit(formData);
            if (response.data.success){
               //do somethnig
            }
         }