当前位置:网站首页>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 .
边栏推荐
- Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
- Is it safe to open an account with flush?
- SQL is used for field data types in various databases
- 揭秘GES超大规模图计算引擎HyG:图切分
- [C language practice - print the upper triangle and its deformation (with blank version)]
- In 2021, China's private equity market is growing, and the scale of private equity fund management reaches 19.78 trillion yuan [figure]
- Why are life science enterprises on the cloud in succession?
- QQ机器人官方插件加载配置方法【beta2版本】
- shell-跳出循环-shift参数左移-函数的使用
- JS some small problems about adding and accessing values to arrays
猜你喜欢

Ali visual AI training camp -day05- creativity day - your image recognition project

Record Baidu search optimization thinking analysis

2020-12-09 laravel . Env file loading mechanism process
![Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]](/img/d1/8ed2958ef365e17494bade6e29ee04.jpg)
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]

Guangzhou Sinovel interactive creates VR Exhibition Hall panoramic online virtual exhibition hall
![Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]](/img/93/fd2cfa315c2f6d232078f7b20a7eb1.jpg)
Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]

Elastic high-performance computing on the cloud supports the rapid development of the life science industry, reducing costs and increasing efficiency
![QQ robot: self forbidden words management of group members [latest beta2 version]](/img/1b/7dcc8ed344c9f62870d76f16b99f1d.png)
QQ robot: self forbidden words management of group members [latest beta2 version]

JVM|运行时数据区(堆空间)

Kotlin compose terminate todo project Click to edit and modify todo
随机推荐
广州华锐互动VR全景为各行各业带来发展
Electronic basic project construction & communication between main thread and rendering thread
Vulnhub range the planes: mercury
JS some small problems about adding and accessing values to arrays
Do you want to know how new investors open accounts? Is online account opening safe?
Many varieties of EA can be used
On Oracle full stack virtual machine -- graalvm
Embark on a new journey and reach the world with wisdom
Redis cache preheating & avalanche & breakdown & penetration
One night I worked as an XPath Terminator: XPath Helper Plus
Vulnhub range - darkhole 1
ActiveMQ--CVE-2016-3088
为什么生命科学企业都在陆续上云?
Solidity contract address to wallet, wallet address to contract
Google SEO external chain releases 50+ website platform sharing (e6zzseo)
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
QQ robot epidemic situation query / epidemic situation concern [latest beta2 version]
Network security detection and prevention test questions (II)
Solidity get quarterly time
Laravel validation rule followed Role of auth:: id()