axios get 请求发送 FormData 数据

axios是一个流行的基于Promise的HTTP客户端库,用于浏览器和Node.js环境中。如果你想在axios发起GET请求时发送FormData数据,通常会在`axios.get()`方法中设置`params`或`data`属性。

如果你要发送的是普通的键值对数据,可以像下面这样:

javascript 复制代码
const formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

axios.get('/api/endpoint', {
  params: formData, // 将FormData作为查询参数传递
  headers: {'Content-Type': 'application/x-www-form-urlencoded'} // 设置正确的content-type
})
.then(response => console.log(response.data))
.catch(error => console.error(error));

如果你的数据是非表单结构,并且需要通过`multipart/form-data`格式发送,那么应该直接将`FormData`实例作为`data`:

javascript 复制代码
axios.get('/api/endpoint', {
  data: formData,
  headers: {'Content-Type': 'multipart/form-data'}
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
相关推荐
海市公约2 天前
Axios网络请求库核心特性与拦截器封装实践
axios·前端开发·promise·拦截器·网络封装
xChive9 天前
前端请求取消:用 AbortController 从 fetch 到 axios
前端·vue.js·axios·fetch·abortcontroller
辞忧九千七14 天前
前后端分离架构实战与项目落地:AI智能学习笔记管理系统
python·html·axios·css3·fastapi·dify
rising start14 天前
InsightEdu - 轻量智能学习平台
javascript·axios·css3·html5·fastapi·orm·dify
Forget the Dream18 天前
基于适配器模式的 Axios 封装实践
设计模式·typescript·axios·适配器模式
90后的晨仔21 天前
SwiftUI 数据持久化完全指南:从偏好设置到企业级存储
ios·axios
LIO23 天前
Axios Token 无感刷新机制:原理、实现与最佳实践
前端·axios
芳草萋萋鹦鹉洲哦23 天前
【tauri】为什么接口通信选择invoke而不是Axios
rust·axios·tauri·invoke
吴声子夜歌1 个月前
Vue3——网络框架Axios的应用
javascript·vue3·axios
wordbaby1 个月前
一次跨端 Loading 卡死复盘:把请求计数从 Axios 拦截器迁到 try/catch/finally
前端·axios