vuejs如何利用form-data发送请求
vuejs项目开发中,如何利用form-data发送请求,下面编程教程网小编给大家简单介绍一下具体实例!
安装脚手架:
npm install axios
示列代码:
<template>
<div>
<form>
<input type="text" v-model="name" />
<input type="file" ref="file" />
<button @click.prevent="submitForm">Submit</button>
</form>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'FormDataExample',
data() {
return {
name: '',
};
},
methods: {
async submitForm() {
const formData = new FormData();
formData.append('name', this.name);
formData.append('file', this.$refs.file.files[0]);
try {
const response = await axios.post('/api/submit-form', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
console.log(response);
} catch (error) {
console.error(error);
}
},
},
};
</script>
以上是编程学习网小编为您介绍的“vuejs如何利用form-data发送请求”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
