当前位置:网站首页>Sending network request in wechat applet
Sending network request in wechat applet
2022-06-23 21:41:00 【richest_ qi】
List of articles
Applet project
The main files involved in the code are :
- app.json
- pages/index/index.wxml
- pages/index/index.wxss
- pages/index/index.js

app.json
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#1c424d",
"navigationBarTitleText": " home page ",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
pages/index/index.html
<view class="box">
<image src="/static/images/aito.jpg"></image>
<text class="title">AITO Ask boundary M5</text>
<text>{
{desc}}</text>
</view>
pages/index/index.wxss
.box{
margin: 50rpx;
display: flex;
flex-direction: column;
align-items: center;
}
image{
width: 100rpx;
height: 100rpx;
border-radius: 50%;
}
.title{
padding: 20rpx;
font-size: 28rpx;
color: rgba(0,0,0,0.7);
}
pages/index/index.js
Page({
data:{
desc:""
}
})
Send network request
Network request function
Wechat applet provides wx.request(Object object) This API, Used to send network requests , The API Take an object as an argument , The object contains multiple attributes , The common ones are :
url, Requested address ,string type , Required .data, Requested parameters , It can be a string or an object , Not required .method, Requested method ,string type , The default value is"GET".success, Request successful callback function , Not required .fail, Callback function for request failure , Not required .
The documents involved in the code change are :pages/index/index.js.
If you want the test that sends the request to run , You need to make simple settings in the wechat developer tool :
details > Local settings , Check Do not check the legal domain name 、web-view( Business domain name )、TLS Version, and HTTPS certificate .

pages/index/index.js
Page({
data:{
desc:""
},
onLoad(){
wx.request({
url:"https://api.uixsj.cn/hitokoto/get",
method:"GET",
success: res => {
console.log(" The request is successful ",res);
this.setData({
desc:res.data})
},
fail: err => {
console.log(" request was aborted ",err);
}
})
}
})
Encapsulate network request function
The documents involved in the code change are :
- pages/index/index.js.
- utils/request.js. Create a new directory under the project root directory :utils,utils Next new file :request.js.
pages/index/index.js
import request from "../../utils/request";
Page({
data:{
desc:""
},
onLoad(){
this.getDataFromServer();
},
async getDataFromServer(){
let result = await request("https://api.uixsj.cn/hitokoto/get");
console.log(result);
this.setData({
desc:result})
}
})
utils/request.js
export default (url,data={
},method="GET") => {
return new Promise((resolve,reject) => {
wx.request({
url,
data,
method,
success: res => {
console.log(" The request is successful ");
resolve(res.data);
},
fail: err => {
console.log(" request was aborted ");
reject(err);
}
})
})
}
Related links
【 Wechat applet 】 Applet and server http signal communication
边栏推荐
- 同花顺股票开户是安全的吗?
- Xgboost implements text classification and sklearn NLP library tfidfvectorizer
- 小程序ssl证书过期是什么原因导致的?小程序ssl证书到期了怎么解决?
- ES6 promise detailed sewing Red Treasure Book Introduction to ES6 standard
- Troubleshooting the problem that the channel cannot play after easycvr cascades to the upper platform
- Supplementary usage of upload component in fusiondesign 1
- Steps for formulating the project PMO strategic plan
- Embedded development: embedded foundation -- the difference between restart and reset
- Initial experience of nodejs express framework
- What are the advantages of attaching a virtual machine to a hard disk cloud server
猜你喜欢

How does PMO select and train project managers?

Minimisé lorsque Outlook est allumé + éteint

智能座舱SoC竞争升级,国产7nm芯片迎来重要突破

Beitong G3 game console unpacking experience. It turns out that mobile game experts have achieved this

Embedded development: embedded foundation -- the difference between restart and reset

Selenium batch query athletes' technical grades

《阿里云天池大赛赛题解析》——O2O优惠卷预测

Find My资讯|苹果可能会推出第二代AirTag,试试伦茨科技Find My方案

大一女生废话编程爆火!懂不懂编程的看完都拴Q了

Four aspects of PMO Department value assessment
随机推荐
Who do you want to open a stock account? Is it safe to open an account online?
What hard disk does the ECS use? What are the functions of the ECS
小程序ssl证书过期是什么原因导致的?小程序ssl证书到期了怎么解决?
Lighthouse open source application practice: snipe it
MySQL advanced development
How to calculate individual income tax? You know what?
Data visualization: summer without watermelon is not summer
Global and Chinese market of gas fire pit 2022-2028: Research Report on technology, participants, trends, market size and share
SAP retail transaction code mp38 can perform forecasts for multiple items
Open source C # WPF control library -newbeecoder UI usage guide (I)
How to create cloud disk service how to create cloud disk service backup?
What is the gold content of PMP certificate
Coding website hosting migration Tencent cloud cloud development webify
Infrastructure splitting of service splitting
Nodejs operation state keeping technology cookies and sessions
How to gradually improve PMO's own ability and management level
Bi-sql index
What are the processing methods for PPT pictures
How can the cloud disk service be connected to the server? How many hard disks can the server mount?
Cloud database smooth disassembly scheme