当前位置:网站首页>Simple encapsulation of wechat applet wx.request
Simple encapsulation of wechat applet wx.request
2022-07-24 14:54:00 【To_ Zhen】
The initiation is provided in the applet https The method of network request wx.request(Object)
Directly in js Use this method in the file But you still have to use encapsulation for projects
wx.request({
url: 'http://www.ibugthree.com/oldcar/searchCar',
method: 'POST',
data: {
page: 1,
mod: ' BMW '
},
header: {
'pc-token': '4a82b23dbbf3b23fd8aa291076e660ec',
'Accept': 'application/json'
},
success: function (res) {
console.log(res)
}
}
})There may be a mistake

To solve this problem is to details -> Local settings -> Do not verify legal domain name This can be easily solved But you can't do this in projects in the company , The company will give you legal http domain name
The following is wx.request Encapsulation
First, in the pages In the new api The folder is in api New inside http.js and api.js file

http.js
const pubUrl = "http://www.ibugthree.com" // This is the common part of the data interface I want to request
//options Is the receive parameter
const http = (options) => {
// I don't know here. You can enter it to have a look
// console.log(options)
return new Promise((resolve, reject) => {
wx.request({
// If you don't know the parameters and how to transmit them, you can options Take a look at the output
url: pubUrl + options.url, // Splicing request address pubUrl It's the public part options.url Is the address of the requested part
method: options.method, // Type of request received Into the background
data: options.data.page, // Pass in the parameter
header: options.header || {
'Accept': 'application/json',
'pc-token': '4a82b23dbbf3b23fd8aa291076e660ec',
},
success:resolve,
fail:reject
})
})
}
// Put this http Exposed
export default httpapi.js
import http from './http' // Introduce the request method encapsulated above
// For more details , Need to pass parameters No parameters, no transmission data I don't have to
const getCarList = (page) => {
return http({
url:'/oldcar/searchCar',
data:{page},
method:'POST' // Request type
})
}
// Export method , Realize reuse
export default{
getCarList
}
The page that needs to request data js file , Such as :index.js
// For the method of requesting data promise encapsulation , So we need to cooperate with async and await To get to the promise Data in
async onLoad() {
// Call in the method that needs to request the interface to obtain data api The method in
let res = await Api.getCarList({ page:1,mod: ' Mercedes '});
// Here you can output to see if you have obtained data
console.log(res)
// Add to array here
this.data.carList.push(res.data)
},So it's sealed , Add it to the array and you will know how to use it
边栏推荐
- Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
- LeetCode·每日一题·1184.公交站间的距离·模拟
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
- Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
- 打假Yolov7的精度,不是所有的论文都是真实可信
- TS learning record (I) sudo forgets the password (oolong) try changing the 'lib' compiler option to include 'DOM'
- Automated penetration scanning tool
- ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
- Detailed explanation of address bus, data bus and control bus
- 佣金哪家券商最低,我要开户,手机上开户安不安全
猜你喜欢

Similarities and differences between nor flash and NAND flash

Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%

LeetCode·每日一题·1184.公交站间的距离·模拟
![Property datasource is required exception handling [idea]](/img/f3/50d36ed9445695c02e2bd622109d66.png)
Property datasource is required exception handling [idea]

Overview of dobesie wavelet (DB wavelet function) in wavelet transform

Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection

文件操作详解

Decrypt "sea Lotus" organization (domain control detection and defense)

《Route planning method for UAV in unknown environment based on improved SAS algorithm》翻译

LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
随机推荐
记不住正则表达式?这里我整理了99个常用正则
Activate the newly installed Anaconda in the server
Property datasource is required exception handling [idea]
[NLP] next stop, embossed AI
DS diagram - the shortest path of the diagram (excluding the code framework)
Performance test - analyze requirements
Spark: specify the date and output the log of the corresponding date (entry level - simple implementation)
基于ABP实现DDD--实体创建和更新
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
Can you buy 6% of financial products after opening a stock account?
Performance test - Preparation of test plan
Sword finger offer II 001. integer division
LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
Google Earth engine - use MODIS data to export the fire area of monthly data
Meaning of 7 parameters of thread pool
pytorch with torch.no_grad
Not configured in app.json (uni releases wechat applet)
Extjs4 instance address and Chinese document address
DDD based on ABP -- Entity creation and update
REST风格