当前位置:网站首页>微信小程序---网络数据请求
微信小程序---网络数据请求
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 请求”,而是叫做“发起网络数据请求”。
边栏推荐
- 2022年最新北京建筑安全员模拟题库及答案
- 修改mysql数据库root用户的密码
- DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
- Bugku login2
- Is it safe for CICC fortune to speculate in stocks? The securities company with the cheapest handling fee
- 2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
- Summary of key knowledge of C language
- 十周岁生日快乐,Clojure
- [tool sharing] automatic generation of file directory structure tool mddir
- 博途PLC顺序开关机功能块(SCL)
猜你喜欢

Bugku login1

Advanced CAD exercises (I)

Implementation of personalized healthy diet recommendation system based on SSM

Mapwithstate of spark streaming state flow

How to use job plug-in type to call a kettle job through ETL scheduling tool taskctl?

Comprehensively design an oppe homepage -- Design of star models

Google Earth engine - merra-2 m2t1nxaer: aerosol daily data set from 1980 to 2022

2022年最新北京建筑安全员模拟题库及答案

PAT甲级 1047 Student List for Course

Yushenle's learning record: the first project of SOC FPGA -hello world
随机推荐
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
Summary of key knowledge of C language
Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
国元期货网上开户安全吗?开户办理流程是怎样的?
山西阳泉一煤矿发生致1人死亡安全事故,被责令停产整顿
共议公共数据开放,“数牍方案”亮相数字中国建设峰会
Implementation of personalized healthy diet recommendation system based on SSM
2022 latest Beijing Construction Safety Officer simulation question bank and answers
First knowledge of OpenGL (3) fragment shader
C语言重点知识总结
Understand │ XSS attack, SQL injection, CSRF attack, DDoS attack, DNS hijacking
How to configure tke cluster node Max pod
Selection of industrial serial port to WiFi port to Ethernet module of Internet of things
PAT甲级 1049 Counting Ones
互联网协议
Mapwithstate of spark streaming state flow
提问征集丨快来向NLLB作者提问啦!(智源Live第24期)
朋友圈如何测试(思维导图)
物联网工业级串口转WiFi转网口转以太网模块的选型
The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact