首页 > 教程
微信小程序原生wx.request简单封装
- 2025-04-07
- 1033 ℃
调用方法
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,
};下一篇:微信小程序登陆封装
相关内容
Excel文件比较器
国内可用的AI大模型工具...
什么是Web 3.0 ?
网站没有配置SSL,访问ht...
阿里云盘达人计划快速升3...
php经典趣味算法
加菲猫令人喷饭的经典...
splice变相跳出forEach循环
-
app申请支付宝移动支付
2024-05-13 1484
-
快速申请微软 ChatGPT 版新必应方法
2024-07-14 1645
-
ChromeAI – 无限量、离线使用的 Chrome Dev 内置 Gemini Nano 大语言模型
2024-07-03 1733
-
微信好友描述功能比备注好用多了
2025-06-22 1387
-
原型继承和 Class 继承
2025-04-07 799
-
人工智能:探索未知领域的科技之光
2024-04-24 1111
-
当我用 DeepSeek 学习、工作和玩,惊艳!含提问攻略、使用实例和心得
2025-04-30 1149
-
免费下载PPT模板的网站来了
2025-03-10 1368
-
用宝塔(BT)来搭建本地PHP环境
2021-04-13 1845
-
可以长按进行倍速播放的本地视频播放器
2025-04-27 3359
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


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