当前位置:网站首页>微信小程序开发之网络数据请求
微信小程序开发之网络数据请求
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请求”的说法,而是称为“ 发起网络数据请求 ”
边栏推荐
- [web technology] 1391 page visualization building tool, previous life and present life
- Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work
- 【iniparser】项目配置工具iniparser的简单使用
- Pymoo learning (5): convergence analysis
- 这种动态规划你见过吗——状态机动态规划之股票问题(上)
- 有孚原力超算,为客户提供定制化高性能计算服务
- [hdlbits questions] Verilog language (3) modules: hierarchy section
- Intouch高级报警(报警筛选)
- Monitor MySQL based on MySQL exporter
- Pymoo learning (7): Parallelization
猜你喜欢

Istio exposes applications to the Internet

Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report

Why learn service grid istio

Actual combat of MySQL database design project of online mall system

小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
![[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!](/img/6d/b037208996ce52016d014062deaa1f.jpg)
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!

小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板

Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal

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

这种动态规划你见过吗——状态机动态规划之股票问题(上)
随机推荐
Internal network planning and design of Yingcheng hospital
Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
Hongmeng - Damiao computing Sketchpad - Introduction
【Web技术】1391- 页面可视化搭建工具前生今世
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!
【919. 完全二叉树插入器】
FPGA based 1080p 60Hz bt1120 interface debugging process record
telnet安装以及telnet(密码正确)无法登录!
某公司网络设计与规划
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
600000 pieces of data are made from March 1 to March 31. Videodate requires starting time from 00:00 to 24:00 on March 1 to 31, which is only for notes
SQL Server 2019 安装教程
Actual combat of MySQL database design project of online mall system
HTTP cache tongtianpian, there may be something you want
i3-status 配置
Monitor MySQL based on MySQL exporter
微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
Full scale and Xuan of C key