首页 > 教程
微信小程序原生wx.request简单封装
- 2025-04-07
- 1065 ℃
调用方法
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文件比较器
Vue项目Element-UI表格el...
解析PHP中的extract()函数
php经典趣味算法
-
Vue底层判断标签的性能优化方法
2025-04-07 1421
-
骚扰电话API接口 开源
2025-04-07 1151
-
你真的会使用百度搜索吗?
2021-11-22 681
-
知乎热榜API、百度热点API、微博热搜API(开源)- 聚合热榜API开源
2025-04-07 1262
-
语音转文字,文字转语音,两极互转
2025-03-10 1202
-
Coze 扣子 - 字节出品的超强 AI Bot 工具教程,免费无限白嫖 GPT4 方法
2024-04-07 2722
-
你选择产品视角还是用户视角?
2024-05-29 1272
-
终于有一款插件来处理B站烦人的广告了
2025-04-27 1111
-
全面开放!开启微软新必应 Bing Chat 聊天 Ai 功能方法 (免费替代 ChatGPT)
2024-04-07 953
-
php批量去除文件bom代码
2024-03-16 1024
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


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