当前位置:网站首页>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 .
边栏推荐
- From the perspective of the core competitiveness of ABT, the Internet giant, who fell first
- Uncover ges super large scale graph computing engine hyg: Graph Segmentation
- R language uses the model of DALEX package_ The profile function interprets the relationship between a continuous feature and the target value Y in multiple classification models based on the conditio
- 广州华锐互动打造VR展厅全景在线虚拟展厅
- Elastic high-performance computing on the cloud supports the rapid development of the life science industry, reducing costs and increasing efficiency
- LeetCode-78-子集
- One night I worked as an XPath Terminator: XPath Helper Plus
- Using flex to implement the Holy Grail layout is as simple as that
- SEO outsourcing reliable company, enterprise SEO outsourcing company which reliable?
- Install spoole
猜你喜欢
PHP Chinese regular
广州华锐互动VR全景为各行各业带来发展
QQ robot epidemic situation query / epidemic situation concern [latest beta2 version]
Android Development Notes - Quick Start (from sqllite to room licentiousness) 2
【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]
Ali vision AI training camp-day01
Vulnhub range the planes:earth
Record Baidu search optimization thinking analysis
随机推荐
QQ机器人官方插件加载配置方法【beta2版本】
Detailed explanation of oauth2 - Introduction (I)
网络安全检测与防范 练习题(三)
[C language practice - print the upper triangle and its deformation (with blank version)]
想知道新股民怎样炒股票开户?在线开户安全么?
Convert word to PDF through libreoffice
Tcp/ip test questions (V)
What are Baidu collection skills? 2022 Baidu article collection skills
六、HikariConfig的配置解析
On location and scale in CNN
On Oracle full stack virtual machine -- graalvm
Vulnhub range - darkhole 1
MySQL view explanation
electron 基础项目搭建 &&主线程和渲染线程的通信
Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
JS some small problems about adding and accessing values to arrays
Paddleocr learning (II) paddleocr detection model training
PHP synchronizes website content to hundreds of websites to improve SEO ranking
Sorting out the latest data mining competition scheme!
Comparison rules of strings in JS