个人技术博客——uni.request的使用总结
| 这个作业属于哪个课程 | FZU_SE_teacherW_4 |
|---|---|
| 这个作业要求在哪里 | 软件工程实践总结&个人技术博客-CSDN社区 |
| 这个作业的目标 | 课程实践总结、分析软件开发模式 |
| 其他参考文献 | uni.request(OBJECT) uni-app官网 、 网络 / 发起请求 / wx.request |
目录
- 一、技术概述
- 二、技术详述
- 2.1基本请求实现
- 2.2网络请求封装
- 三、技术使用过程中遇到的问题和解决过程
- 四、总结
- 五、参考文献
一、技术概述
- 技术用途:
uni.request用于实现小程序与后端服务器之间的数据交互,支持GET、POST等多种请求方式。 - 学习原因:实现微信小程序的数据获取与提交,确保前后端的数据传输与交互。
- 技术难点:处理请求的跨平台兼容性、请求失败的错误捕获和网络请求的优化问题。
二、技术详述
2.1基本请求实现
GET请求示例:
uni.request({ url: 'https://api.example.com/data', method: 'GET', // success和fail回调分别处理请求成功和失败的情况 success: (res) => { console.log(res.data); }, fail: (err) => { console.error(err); } });POST请求示例:
uni.request({ url: 'https://api.example.com/submit', method: 'POST', data: { name: 'John', age: 30 }, success: (res) => { console.log(res.data); }, fail: (err) => { console.error(err); } });
2.2网络请求封装
为了提高代码复用性,通常会对请求进行封装(通常项目会使用token进行登录验证,以下考虑了token验证)
const BASE_URL = 'https://api.example.com'; /* * 全局请求封装 * @param path 请求路径 * @param method 请求类型(GET/POST/DELETE等) * @oaram data 请求体数据 * @param loading 请求未完成是是否显示加载中,默认为true */ export default (path, method, data = {}, loading = true) => { // 从localStorage中获取存储token const token = uni.getStorageSync("token"); if (loading) { uni.showLoading({ //这里需主动调用 uni.hideLoading 才能关闭提示框 title: "加载中", mask: true }); }; //根据有无token调用函数 if (token != '') { return tokenRequest(path, method, data, loading, token) } else { return noTokenRequest(path, method, data, loading) } }; // 有token时发送请求函数 function tokenRequest(path, method, data, loading, token) { return new Promise((resolve, reject) => { uni.request({ url: BASEURL + path, method: method, data, header: { "token": token }, success(response) { //这里可以做响应拦截(以返回401表示未登录为例) if (response.data.code === 401) { logout(); } console.log(response.data); resolve(response.data); }, fail(err) { uni.showToast({ icon: "none", title: '服务响应失败' }); console.error(err); reject(err); }, complete() { uni.hideLoading(); } }); }); } // 无token时发送请求函数 function noTokenRequest(path, method, data, loading) { return new Promise((resolve, reject) => { uni.request({ url: BASEURL + path, method: method, data, success(response) { //这里同样可以做响应拦截 console.log(response.data) resolve(response.data); }, fail(err) { uni.showToast({ title: '服务响应失败' }); console.error(err); reject(err); }, complete() { uni.hideLoading(); } }); }); }调用
import request from '@/utils/request.js'; // 获取用户信息 export const getBaseInfo = () => { return request(`/user/info`, 'GET') } // 登录 export const login = (user) => { return request(`/user/login`, 'POST', user) }

三、技术使用过程中遇到的问题和解决过程
跨域请求问题
小程序开发中可能遇到跨域请求限制,导致请求失败。解决方法是通过后端设置CORS,允许特定来源访问,或者使用uniCloud代理功能来绕过跨域问题。请求失败和错误处理
网络请求有时会因网络不稳定而失败,需要通过fail回调对不同类型的错误进行处理:fail: (err) => { if (err.statusCode === 500) { uni.showToast({ title: '服务器错误,请稍后再试', icon: 'none' }); } else { uni.showToast({ title: '网络错误,请检查网络连接', icon: 'none' }); } }性能优化
请求频繁时可能影响小程序性能,可以通过分页加载、请求合并等方式减少请求次数,提升用户体验。分页请求示例:
uni.request({ url: 'https://api.example.com/data', method: 'GET', data: { page: currentPage, size: pageSize }, success: (res) => { console.log(res.data); } });
四、总结
uni.request能够有效地实现微信小程序与后端之间的数据交互。通过封装请求、处理跨域问题和优化网络请求性能,提升了代码的复用性和系统的稳定性。在遇到请求失败或网络不稳定时,及时捕获并进行错误处理,确保用户获得清晰的反馈。这些技术措施不仅增强了小程序的功能性,也提升了用户体验和开发效率。
五、参考文献
相关推荐
jpressuniapp傻瓜式教程手把手从0开始编写
jpress uniapp 傻瓜式教程,手把手从0开始编写
uni-app uni.request接口封装
uni-app uni.request接口封装 今天在做uni-app项目时,发现在uni-app中 调取后台接口需要大量的重复编辑,就在想能不能封装一个如同Vue项目中的this.$axios.get(url,data).then();格式,这样就减少了很多代码重复!! 封装为如同this.$axios.get(url,data).then();格式 第一步、 我们先在index首页中的组件部分,创建一个js文件; 原创文章 88获赞 3访问量 3596 关注
uni-app登录实例的实现
使用HBuilderX开发登录实例。我的博客主页:https://blog.csdn.net/k571039838k?orderby=UpdateTime
uni.request.zip
uni-app uni.request封装 全局方法,变量配置 接口调用的封装,文件简洁明了,拿来即用
基于Vue的uni-app个人日记App
使用基于Vue的uni-app框架编写的个人日记App,里面含源代码以及报告,代码下载之后可直接到HbuilderX上运行查看结果


