首页 > 教程
微信小程序原生wx.request简单封装
- 2025-04-07
- 1268 ℃
调用方法
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,
};下一篇:微信小程序登陆封装
相关内容
HTML清除浮动的几种方法
为什么您的电脑有弹窗广...
移动硬盘打不开千万别格式化
iOS 可用的可换源小说阅读器
ChromeAI – 无限量、...
简单实用的电脑技巧,提...
为什么你的AI助手总是答...
免费下载PPT模板的网站来了
-
网页上传图片需要旋转的问题
2024-04-23 1002
-
JS正则常用校验大全
2025-04-07 1209
-
NodeJs文本相似度去重脚本
2025-04-07 1315
-
写代码时容易用到的基础函数总结
2025-04-07 1068
-
宝塔面板如何免费使用专业版插件
2024-07-10 2296
-
心中无码,自然高清
2025-03-10 1605
-
微信拍照模糊调用手机相机拍照更清晰
2025-06-22 1536
-
PHP超级Ping API接口 源码
2025-04-07 1410
-
什么是暗网,暗网不是冒险乐园,它是网络世界的禁区
2025-10-24 1100
-
什么是闰秒?这个困扰我50年的问题终于要被解决了?
2024-05-10 1755
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


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