首页 > 教程
微信小程序原生wx.request简单封装
- 2025-04-07
- 1223 ℃
调用方法
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,
};下一篇:微信小程序登陆封装
相关内容
php使用header()函数导出excel表格
带你了解并熟用Deepseek
Safari浏览器内容被地址...
Zend推出新开发套件 PHP...
为什么我国没有ChatGPT那...
Vue项目Element-UI表格el...
Final Cut Pro X软件...
市面常用浏览器锐评,从...
-
阿里云盘达人计划快速升3级指南
2024-05-31 1809
-
云计算在电子商务和零售业中的优势
2024-04-24 1422
-
什么是深网?有危险吗?如何从A-Z进入深网
2024-04-24 2822
-
如何收集竞争对手的信息?
2024-05-29 1374
-
php使用header()函数导出excel表格
2021-07-12 1584
-
加菲猫令人喷饭的经典台词(中英对照版)
2025-03-03 1386
-
离职时必须清理的电脑文件,不然你的秘密就被人看到了
2021-11-22 815
-
微信转账查看对方是否把我删除好友
2025-06-22 2015
-
如何使用php与数据库进行交互
2024-03-04 1392
-
PHP上传URL地址网络文件到服务器
2025-04-07 1234
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


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