当前位置:网站首页>[wechat applet] read the article, data request
[wechat applet] read the article, data request
2022-07-26 12:16:00 【Front end Xiao Liu is not afraid of cattle】
Hello, old man , This is the front-end Xiaoliu not afraid of Niuniu channel , New articles have been updated in the applet series , Above, we explained the global and local configuration of wechat applet , So today, let's learn about the data request of wechat applet , This is for applet interaction and dynamic acquisition of binding data 、 Important operations of change !!
are you ready? ? Then we'll start , Quickly pick up a small notebook and make notes !

List of articles
Data request
My friends know , stay web Front end development has data requests , We have also learned and used Ajax request . And wechat applets also have data requests , however , The data request of wechat applet is different , First web The environment is browser , The environment of the applet is wechat client ,web Cross domain issues need to be considered , Small programs do not need to consider cross domain issues .
meanwhile , The data request of wechat is not called Ajax request , Why? ? because web Terminal Ajax It's based on the object XMLHttpRequest, Wechat applets don't have this object , Therefore, the data request of wechat applet is not Ajax Request the , It is called Network data request , Don't make a mistake ~~
On the role of data requests , In fact, generally, there will be data requests for interactive effects , To transfer data , The user's behavior will be passed through a parameter , Then identify , The applet is making the corresponding presentation to complete the business . Dynamic presentation of data 、 Initialization of data 、 Data requests may be used in internal judgments of the logic layer .
One , Limiting conditions
For security reasons , Wechat official sets two conditions for the interface of data request , as follows .
Only request
HTTPSInterface of typeFirst of all, here
httpThe type andhttpsMake a simple explanation of the differences between types :HTTPIt's the hypertext transfer protocol , It is the most widely used network protocol on the Internet , andHTTPSbe relative toHTTPCome on , It's about securityHTTPpassageway , Simply put, it's the safe version ( Add oneSSLLayer to encrypt ),HTTPSneed ca certificate , Information is not likehttpPlaintext transmission , It issslThe encrypted .The interface domain name must be added to the trust list
The simple understanding is , Wechat applet needs to set the communication domain name in advance , The applet can only communicate with the specified domain name , Safety guaranteed .
The trust list can be found in the wechat developer tool The toolbar –> details –> Project settings –>request Legitimate domain name View in .

request Legitimate domain nameThe default is empty. .So how to add , Look below .
Two , To configure request Legitimate domain name
It's simple , We only need to log in wechat applet to manage the background , And then you can go in Development –> Development and setup –> Server domain name Modified .
If this is the first configuration , Authentication is also required .
There are some things you should pay attention to about domain names tips
- Must be
httpstype - Out of commission
ipandlocalhostDomain name of - Domain name needs to be approved by
ICPKeep on record - You can only modify the server domain name five times a month
The domain name has been set , How should we send the request ?
3、 ... and , launch GET、POST request
In the wechat applet , Network data requests are made through top-level objects wx Of request() Method to send the request , such as GET request .
The structure is as follows :
wx.request({
url : " domain name ", // Need to be in the trust list
method : "GET",
data : {
// Put the requested data
},
success : (res)=>{
// Callback function called after successful request
}
})
POST The request structure is similar to GET No difference , Only need to method To change the value of POST that will do , Bloggers here don't do more demonstrations .
Four , The page has just loaded the request data
occasionally , We need to implement when the page is loaded , Make a data request , To help render the initial picture of the page ,“ Don't write dead ” A lot of data is requested once when the page is loaded .
And how should this be achieved , In fact, we can use the onload event , When the page is loaded, the event is triggered , Then we put the event handler in it , Our data request can be found in the event handler , In this way, you can request data when the page is loaded .
onLoad: function(options){
// Data request operation
}
5、 ... and , skip request Legal domain name verification
When developing a project , The front and rear progress cannot be determined , Sometimes there will be , The backend partners only provide http Interface of type , did not https Interface of type , The front end cannot be debugged , It will affect the progress of the project .
Now , We can use wechat developer tools , Temporarily enabled in local settings [ The development environment does not verify the requested domain name 、TLS Version and HTTPS certificate Button , As shown in the following square diagram , After opening , When opening an applet with wechat developer tools or mobile phones , The server domain name will not be verified 、TLS Version and HTTPS Certificate verification , That means we can use http Interface to protocol .
But here's the thing , The online version still needs to verify the server domain name , The above is only for developers .

That's the end of today's applet article , If it helps you , You can follow Niuniu's next article , Thank you for your support , Your support is the greatest motivation for my creation !!!
Debt see ~~
边栏推荐
- Flutter's learning path
- 三维点云课程(八)——特征点匹配
- Some common writing methods and skills
- uniapp h5、app引用外部在线js
- Do you need to launch MES system? What are you struggling with?
- 面试京东T5,被按在地上摩擦,鬼知道我经历了什么?
- [Anhui University] information sharing of postgraduate entrance examination and re examination
- 大量if else判断如何优化?@Valib详解
- Koin
- pytest接口自动化测试框架 | 使用多个fixture
猜你喜欢
随机推荐
行业案例|指标中台如何助力银行业普惠金融可持续发展
SSJ-21B时间继电器
羽毛球馆的两个基础设施你了解多少?
Hou Peixin, chairman of the openharmony Working Committee of the open atom open source foundation, sent a message to the openatom openharmony sub forum
pytest接口自动化测试框架 | pytest的setup和teardown函数
RFID的工作原理
Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)
[MySQL constraint]
面试官:如何理解QPS,TPS,RT?
什么是物联网?常见IoT协议最全讲解
On the construction and management of low code technology in logistics transportation platform
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
[Anhui University] information sharing of postgraduate entrance examination and re examination
基于STM32的SIM900A发送中文和英文短信
FPGA入门学习(一) - 第一个FPGA工程
三维点云课程(八)——特征点匹配
transformer一统天下?depth-wise conv有话要说
Ubenwa, a start-up under Mila, received an investment of US $2.5 million to study the AI diagnosis of infant health
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
11 "pocket" universities in China! Running on campus and leaving the school before accelerating








