当前位置: 首页 > news >正文

【uni-app】axios 报错:Error: Adapter ‘http‘ is not available in the build

在 uni-app 中使用 axios 会报错:Error: Adapter ‘http‘ is not available in the build

解决方法:为 axios 添加 adapter 适配器。

import axios from 'axios';
import settle from '../../node_modules/axios/lib/core/settle';
import buildURL from '../../node_modules/axios/lib/helpers/buildURL';// 设置基础URL
axios.defaults.baseURL = 'http://localhost:3000';// 解决 uniapp 适配axios请求,避免报adapter is not a function错误
axios.defaults.adapter = function (config) {const { method } = config;return new Promise((resolve, reject) => {uni.request({url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),method: method?.toUpperCase(),header: { ...config.headers },data: config.data,responseType: config.responseType,complete: function complete(response) {const { data, statusCode, errMsg, header } = response;const responseInfo = {data,status: statusCode,errMsg,header,config: config,};settle(resolve, reject, responseInfo);},});});
};// 请求拦截器
axios.interceptors.request.use(config => {const token = uni.getStorageSync('token');if (token) {config.headers['Authorization'] = `Bearer ${token}`;}return config;},error => {return Promise.reject(error);}
);// 响应拦截器
axios.interceptors.response.use(response => {return response;},error => {// 处理401错误(未授权)if (error.response && error.response.status === 401) {uni.removeStorageSync('token');uni.removeStorageSync('user');uni.showToast({title: '登录已过期,请重新登录',icon: 'none'});window.location.href = '/login';}return Promise.reject(error);}
);// 认证相关API
export const authAPI = {register: (userData) => axios.post('/api/auth/register', userData), // 已使用login: (credentials) => axios.post('/api/auth/login', credentials), // 已使用getCurrentUser: (object) => axios.post('/api/auth/me', object), // 已使用updateProfile: (profileData) => axios.put('/api/auth/profile', profileData)
};

相关文章:

  • cursor如何集成MCP服务
  • 爬虫: 一文掌握 pycurl 的详细使用(更接近底层,性能更高)
  • oracle查询锁表和解锁
  • 第十八讲 | 支持向量机(SVM):在地类识别与遥感影像分类中的应用
  • Spark-SQL简介及核心编程
  • [AI ][Dify] 构建一个自动化新闻编辑助手:Dify 工作流实战指南
  • Spark-SQL核心编程(一)
  • Java 设计模式:组合模式详解
  • 体系结构论文(六十七):A Machine-Learning-Guided Framework for Fault-Tolerant DNNs
  • GpuGeek:重构AI算力基础设施,赋能产业智能升级
  • 大数据面试问答-批处理性能优化
  • 快速排序(非递归版本)
  • 【3D文件】可爱小鹿3D建模,3D打印文件
  • 五大生产模式(MTS、MTO、ATO、ETO、CTO)的差异
  • AIoT 智变浪潮演讲实录 | 刘浩然:让硬件会思考:边缘大模型网关助力硬件智能革新
  • 001 蓝桥杯嵌入式赛道备赛——基础
  • [特殊字符]飞牛相册测评:智能相册界的宝藏还是鸡肋?
  • Coze 和 n8n 的详细介绍及多维度对比分析,涵盖功能、架构、适用场景、成本等关键指标
  • 鸿蒙NEXT开发Emitter工具类(ArkTs)
  • 操作系统学习笔记——[特殊字符]超详细 | 如何唤醒被阻塞的 socket 线程?线程阻塞原理、线程池、fork/vfork彻底讲明白!
  • 耗资10亿潮汕豪宅“英之园”将强拆?区政府:非法占用集体土地
  • 世贸组织欢迎中美经贸高层会谈取得积极成果
  • 国家统计局今年将在全国开展两次人口固定样本跟访调查
  • 上海与世界|环城生态公园带是上海绿色发展新名片
  • 举牌代跳明码标价、留言不堪入目,未成年人擦边短视频成引流利器
  • “海豚音”依旧互动更多,玛丽亚·凯莉本周来沪开唱