首页 > 教程
微信小程序原生wx.request简单封装
- 2025-04-07
- 1101 ℃
调用方法
import { get, post } from "../../request/request";
// GET请求
const _res = await get("https://api.vvhan.com/api/ian");
console.log(_res);
// POST请求
const _res = await post("https://api.vvhan.com/api/ian");
console.log(_res);request.js
const request = (url, options) => {
return new Promise((resolve) => {
options.isLoading &&
wx.showLoading({
title: "正在加载",
});
wx.request({
url,
method: options.method,
data: options.data,
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
success(res) {
resolve(res.data);
options.isLoading && wx.hideLoading();
},
fail(error) {
options.isLoading && wx.hideLoading();
wx.showToast({
icon: "none",
title: "请求失败",
duration: 1400,
});
},
});
});
};
const get = (url, options = {}, isLoading = true) => {
return request(url, {
method: "GET",
data: options,
isLoading,
});
};
const post = (url, options = {}, isLoading = true) => {
return request(url, {
method: "POST",
data: options,
isLoading,
});
};
module.exports = {
get,
post,
};下一篇:微信小程序登陆封装
相关内容
图小小 – 开源、批...
如何切割长图打印成 A4...
php使用header()函数导出excel表格
教你查询手机号绑定了哪...
从c++魔板算法开始研究加...
推荐7款免费好用的国内外...
这些搜索方法可以助你事...
3个DeepSeek隐藏玩法,99...
-
Ant Design Vue 4.0 侧边导航栏Menu组件封装
2025-04-07 1009
-
PHP模拟浏览器上传文件(数据流形式上传)
2025-04-07 873
-
百度站长平台不刷新DNS信息
2024-05-10 1134
-
微信最致命的查岗功能,查对象往祖坟上刨
2025-06-22 1438
-
php结合redis实现高并发下的抢购、秒杀功能
2021-07-27 898
-
微信深度清理,一次清理好几个G
2025-03-04 1430
-
DeepSeek最强使用攻略,放弃复杂提示词,直接提问效果反而更好?
2025-04-30 1327
-
JQUERY判断一个元素是否在可视区域中
2024-03-20 1415
-
坐骨股骨撞击综合征的MRI诊断价值研究
2024-06-18 1403
-
很上档次别具一格的代码注释方式
2025-04-07 1247
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


进入有缘空间
点击分享文章