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 内容,请继续关注编程基础学习网。
编程基础网
本文标题为:vuejs如何利用form-data发送请求
基础教程推荐
猜你喜欢
- Ajax异步请求技术实例讲解 2023-02-14
- 关于带有”显示更多”按钮的多行文本截断思考 2024-01-16
- element-ui el-dialog嵌套table组件,ref问题及解决 2024-03-09
- html5的websockets全双工通信详解学习示例 2023-12-27
- vuejs如何实现列表无缝滚动 2025-01-12
- 6.滚动标签.html 2023-10-26
- JS网页repaint与reflow 的区别及优化方式 2023-12-19
- 使用 IntraWeb (28) - 基本控件之 TIWTemplateProcessorHTML、TIWLayoutMgrHTML、TIWLayoutMgrForm 2023-10-26
- Ajax 向数据库修改和添加功能(较简答) 2023-02-01
- django admin 使用SimpleUI自定义按钮弹窗框示例 2024-03-08
