当前位置:网站首页>微信小程序开发之网络数据请求
微信小程序开发之网络数据请求
2022-07-25 19:24:00 【Aricl.】
目录
二、配置服务器合法域名
三、发起请求
一、限制
出于安全性考虑,小程序官方对数据接口的请求做出了如下两点限制:
- 只能请求HTTPS类型的接口
- 必须先将接口的域名添加到信任列表中

二、配置服务器合法域名
配置步骤:
- 登录微信小程序管理后台 链接
- 点击开发
- 开发管理
- 开发设置
- 服务器域名
- 点击右上角修改requst合法域名


注意事项:
- 域名只支持https协议
- 域名不能使用IP地址或者localhost
- 域名必须经过ICP备案(后端了解)
- 服务器域名一个月内最多可申请5次修改(官网信息有误,社区工作人员已经做出解释)

三、发起请求
(1)GET请求
//发起GET请求
getInfo(){
wx.request({
url: 'https://XXX',//所要请求的接口地址
method:'GET',//请求的方式
data:{
name:'XX',
age:20
},
//请求成功的回调函数
success:(res)=>{
console.log(res)//打印服务器返回的对象
},
//请求失败的回调函数
fail:(res)=>{
console.log('请求数据失败!')
},
//无论请求成功与否都会执行的函数
complete:(res)=>{
console.log('已经发送网络数据请求!')
}
})
},(2)POST请求
//发起POST请求
getInfo(){
wx.request({
url: 'https://XXX',//所要请求的接口地址
method:'POST',//请求的方式
data:{
name:'XX',
age:20
},
//请求成功的回调函数
success:(res)=>{
console.log(res)//打印服务器返回的对象
},
//请求失败的回调函数
fail:(res)=>{
console.log('请求数据失败!')
},
//无论请求成功与否都会执行的函数
complete:(res)=>{
console.log('已经发送网络数据请求!')
}
})
},(3)二者区别
如果你要提交一篇文章,肯定只能用post,因为数据量太大,get请求类似浏览器url请求,你把一篇文章放到url里面肯定请求不过去,如果你只是个简单的传id或者比较少的参数可以用get,还有就是有密码输入建议用post,post请求参数不会以明文显示在url上。
一句话:GET请求可以把参数包含在URL中,但POST请求需要通过request body来传递参数。
参考文章:GET与POST二者的通俗化解释
四、 跳过requst合法域名校验
如果在开发中,后端程序员仅仅提供了http协议的接口,暂时没有提供https协议的接口,此时为了不耽误项目开发进度,可以现在微信开发者工具的项目详情里面的本地设置,将【不校验合法域名、web-view(业务域名)、TLS版本以及HTTPS证书】勾选上,以此来暂时跳过合法域名的检测机制(这一操作仅限在开发与调试阶段使用!)

五、关于跨域和Ajax的说明
(1)跨域问题只存在于基于浏览器的Web开发中。由于小程序的宿主环境是微信客户端,所以小程序中是不存在跨域问题的。
(2)Ajax技术的核心是依赖于浏览器中的XML HttpRequst这个对象,由于小程序的宿主环境是客户端,所以小程序中没有“ 发起Ajax请求”的说法,而是称为“ 发起网络数据请求 ”
边栏推荐
- [encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!
- JS basic type reference type deep / shallow clone copy
- SQL Server 2019 安装教程
- How to analyze qiime2 after obtaining picrust2 results
- 【Web技术】1391- 页面可视化搭建工具前生今世
- The finished product of wechat campus maintenance and repair applet graduation design (1) development outline
- 【919. 完全二叉树插入器】
- 基于PHP的中非南南合作信息交流平台网站建设
- [iniparser] simple use of the project configuration tool iniparser
- 微信小程序 26 播放音乐页的完善②
猜你喜欢

帝国CMS整站|手机号/QQ靓号商城源码|适配移动端

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...

Pymoo学习 (5):收敛性分析

Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee

Improvement of wechat applet 29 hot search list ②

Wechat campus maintenance application applet graduation design finished product of applet completion work (3) background function

Istio exposes applications to the Internet

Sccm2012r2 network deployment reinstallation system

【iniparser】项目配置工具iniparser的简单使用

某公司网络设计与规划
随机推荐
虹科分享|如何解决勒索软件安全漏洞
Telnet installation and telnet (correct password) cannot log in!
Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
Eve - 0day Threat Intelligence
Monitor MySQL based on MySQL exporter
Improvement of wechat applet 28 hot search list ①
How to be a self disciplined person?
网上商城系统MySql数据库设计项目实战
Youfu force supercomputing provides customized high-performance computing services for customers
基于FPGA的1080P 60Hz BT1120接口调试过程记录
【小程序开发】常用组件及基本使用详解
阿里云免费SSL证书申请详细流程
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
Sccm2012r2 network deployment reinstallation system
Imeta | sangerbox: interactive integrated clinical information analysis platform
[applet development] detailed explanation of host environment
Basic mode of music theory
QIIME2得到PICRUSt2结果后如何分析
iMeta | Sangerbox: 交互式整合临床生信分析平台
【刷题记录】21. 合并两个有序链表