当前位置:网站首页>Applet wx Request encapsulation
Applet wx Request encapsulation
2022-06-25 19:33:00 【Bin daotianxia】
An example of an applet's official interface :
wx.request({
url: 'test.php', // Just for the sample , Not a real interface address
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' // The default value is
},
success: function(res) {
console.log(res.data)
}
})among ,url The URL section of the ,header part ,method: 'POST', It's repetitive , Write every page wx.request Very trouble .
1. Create a new one js( Usually placed in small programs utils Under the folder ). Name at will ( My name is wxrequest.js).
var app = getApp();
function request(url, postData, doSuccess, doFail, doComplete) {
var api_url = app.globalData.api_url;
wx.request({
url: api_url + url,
data: postData,
method: 'POST',
success: function(res) {
if (typeof doSuccess == "function") {
doSuccess(res);
}
},
fail: function() {
if (typeof doFail == "function") {
doFail();
}
},
complete: function() {
if (typeof doComplete == "function") {
doComplete();
}
}
});
};
module.exports.request = request;2. Introduce this file at the top of the page that needs to invoke the interface .

3. Call the interface in the page , Write directly in this way
request.request(
'adp', // Need to pass to wxrequest.js Of url
{pos: 'HOT'}, // It needs to be passed on data
function(res) { // The transmission was successful , What comes out of the interface .
// What to do after the interface call is successful .
console.log('QQ39560458',res.data)
if (res.data.err == 0) {
that.setData({
adp: res.data.data.imgs
})
}
}
)When the data comes out, you can assign values directly .
What don't you understand , Feel free to leave a comment below .
边栏推荐
- PostgreSQL user role permissions
- 为什么生命科学企业都在陆续上云?
- Vulnhub range - correlation:2
- shell-跳出循环-shift参数左移-函数的使用
- DataX script task development record
- Tcp/ip test questions (I)
- MySQL transaction explanation
- 6、 Configuration resolution of hikariconfig
- Principles of MySQL clustered index and non clustered index
- PostgreSQL change table owner
猜你喜欢

Connecting PHP to MySQL instances in the lamp environment of alicloud's liunx system

谈谈CNN中的位置和尺度问题

Web development solution to cross domain problems
![Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]](/img/e0/d4ed9a9d91534be0d956414f6abaaa.jpg)
Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]

Bloom filter

DARKHOLE 2

Detailed explanation of oauth2 - Introduction (I)

ECS 7-day practical training camp (Advanced route) -- day04 -- build a portal using ECs and polardb

mysql load data infile

Record Baidu search optimization thinking analysis
随机推荐
Idea common plug-ins
网络安全检测与防范 练习题(三)
Bloom filter
wooyun-2014-065513
Redis cache preheating & avalanche & breakdown & penetration
JS get data
PHP FPM, workman, spoole, golang simple performance test
Comparison rules of strings in JS
LeetCode-78-子集
最新数据挖掘赛事方案梳理!
Network security detection and prevention exercises (III)
Guangzhou Sinovel interactive creates VR Exhibition Hall panoramic online virtual exhibition hall
What should I pay attention to in GoogleSEO content station optimization?
Solidity contract address to wallet, wallet address to contract
Analysis on development scale and development trend of China's night economy industry in 2021 [figure]
QQ robot epidemic situation query / epidemic situation concern [latest beta2 version]
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
LeetCode-101-对称二叉树
Analysis on the market scale and pattern of contrast agents in China in 2021: Jiangsu Hengrui pharmaceutical, general electric, Yangzijiang Pharmaceutical Group, Bayer and bleco account for more than
Vulnhub range the planes: mercury