当前位置:网站首页>微信小程序---网络数据请求
微信小程序---网络数据请求
2022-07-26 16:06:00 【前端小白在前进】
学习笔记
阿里云盘 : https://www.aliyundrive.com/s/5Zrfw9EpKwa
提取码:37sc
网络数据请求
小程序中网络数据请求的限制
处于安全性方面的考虑,小程序官方对数据接口的请求做出了如下两个限制:
- 只能请求
https类型的接口 - 必须将接口的域名添加到信任列表中

配置request合法域名
需求描述:假设在自己的微信小程序中,希望请求 https://www.escook.cn/ 域名下的接口
配置步骤:登录微信小程序管理后台 -> 开发 -> 开发设置 -> 服务器域名 -> 修改 request 合法域名
注意事项:
- 域名只支持https协议
- 域名不能使用ip地址或localhost
- 域名必须经过icp备案
- 服务器域名一个月内最多可申请5次修改
发起get请求
调用微信小程序提供的 wx.request() 方法,可以发起 GET 数据请求,示例代码如下:
发起post请求
调用微信小程序提供的 wx.request() 方法,可以发起 POST 数据请求,示例代码如下:
在页面加载时请求数据
在很多情况下,我们需要在页面刚加载的时候,自动请求一些初始化的数据。此时需要在页面的 onLoad 事件中调用获取数据的函数,示例代码如下:
跳过request合法域名校验
如果后端程序员仅仅提供了 http 协议的接口、暂时没有提供 https 协议的接口。此时为了不耽误开发的进度,我们可以在微信开发者工具中,临时开启「开发环境不校验请求域名、TLS 版本及 HTTPS 证书」选项,跳过 request 合法域名的校验。
注意:跳过 request 合法域名校验的选项,仅限在开发与调试阶段使用!
关于跨域和Ajax的说明
跨域问题只存在于基于浏览器的 Web 开发中。由于小程序的宿主环境不是浏览器,而是微信客户端,所以小程序中不存在跨域的问题。
Ajax 技术的核心是依赖于浏览器中的 XMLHttpRequest 这个对象,由于小程序的宿主环境是微信客户端,所以小程序中不能叫做“发起 Ajax 请求”,而是叫做“发起网络数据请求”。
边栏推荐
- From SiCp to LISP video replay
- Pat grade a 1045 favorite color stripe
- PAT甲级 1047 Student List for Course
- Understand │ XSS attack, SQL injection, CSRF attack, DDoS attack, DNS hijacking
- Jmeter快速上手之接口测试
- PAT甲级 1044 Shopping in Mars
- zabbix 6.2.0部署
- SQL statement -- single line comment and multi line comment
- 2022 latest Beijing Construction Safety Officer simulation question bank and answers
- Simulation of three-phase voltage source inverter based on SISOTOOL pole assignment PI parameters and pless
猜你喜欢

The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact

Google Earth Engine——MERRA-2 M2T1NXAER:1980-2022年气溶胶逐日数据集

2022 what is your sense of security? Volvo asked in the middle of the year

Sql语句——单行注释与多行注释

Pat class a 1047 student list for course

Parker solenoid valve d1vw020dnypz5

博途PLC顺序开关机功能块(SCL)

【ARM学习(9) arm 编译器了解学习(armcc/armclang)】

Mapwithstate of spark streaming state flow

Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
随机推荐
PAT甲级 1046 Shortest Distance
First knowledge of OpenGL (4) link shader
初识OpenGL (2)编译着色器
How to configure tke cluster node Max pod
2022年最新北京建筑安全员模拟题库及答案
First knowledge of OpenGL (3) fragment shader
综合设计一个OPPE主页--导航栏的设计
Comprehensively design an oppe homepage -- Design of star models
2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
Understanding weight sharing in convolutional neural networks
技术风向标 | 云原生技术架构成熟度模型解读
FTP protocol
Pat grade a 1045 favorite color stripe
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成
We were tossed all night by a Kong performance bug
基于sisotool极点配置PI参数及基于Plecs的三相电压源逆变器仿真
Activity之onCreate、onRestoreInstanceState恢复数据的区别
Finally, someone explained the red blue confrontation clearly
请问一下各位大佬,mysql-cdc建表如何指定表的字符集呢,在官网没找到相应的连接器参数。我读一个
2022 what is your sense of security? Volvo asked in the middle of the year