微信小程序 5 网络api
2017-09-12 23:07
399 查看
wx.request 发起网络请求
data 最后一定是字符串的形式
header 请求头
success 成功后执行的回调函数
返回一个
上传
file.path 上传路径
返回一个
progress 上传进度百分比
totalBytesSent 上传的数据长度 单位为bytes
totalBytesExpectedToSend 预计要上传的数据长度 单位同上
uploadTask 对象的方法列表:
.
url 下载资源的地址
返回一个
属性同上传
websocket
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。
一个微信小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。
url 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名
header 请求头
protocols 子协议数组
method get请求
wx.connectSocket,并在
wx.onSocketOpen 回调之后才能发送。
wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content-type': 'application/json' // 默认值 }, success: function(res) { console.log(res.data) } })
data 最后一定是字符串的形式
header 请求头
success 成功后执行的回调函数
返回一个
requestTask对象,通过
requestTask,可中断请求任务。
onst requestTask = wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content-type': 'application/json' }, success: function(res) { console.log(res.data) } }) requestTask.abort() // 取消请求任务
上传
wx.uploadFile
wx.chooseImage({ success: function(res) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址 filePath: tempFilePaths[0], name: 'file', formData:{ 'user': 'test' }, success: function(res){ var data = res.data //do something } }) } })
file.path 上传路径
返回一个
uploadTask对象,通过
uploadTask,可监听上传进度变化事件,以及取消上传任务。
const uploadTask = wx.uploadFile({ url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址 filePath: tempFilePaths[0], name: 'file', formData:{ 'user': 'test' }, success: function(res){ var data = res.data //do something } }) uploadTask.onProgressUpdate((res) => { console.log('上传进度', res.progress) console.log('已经上传的数据长度', res.totalBytesSent) console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend) }) uploadTask.abort() // 取消上传任务
progress 上传进度百分比
totalBytesSent 上传的数据长度 单位为bytes
totalBytesExpectedToSend 预计要上传的数据长度 单位同上
uploadTask 对象的方法列表:
方法 | 参数 | 说明 | 最低版本 |
---|---|---|---|
onProgressUpdate | callback | 监听上传进度变化 | 1.4.0 |
abort | 中断上传任务 | 1.4.0 |
wx.downloadFile下载文件到本地
wx.downloadFile({ url: 'https://example.com/audio/123', //仅为示例,并非真实的资源 success: function(res) { wx.playVoice({ filePath: res.tempFilePath }) } })
url 下载资源的地址
返回一个
downloadTask对象,通过
downloadTask,可监听下载进度变化事件,以及取消下载任务。
const downloadTask = wx.downloadFile({ url: 'http://example.com/audio/123', //仅为示例,并非真实的资源 success: function(res) { wx.playVoice({ filePath: res.tempFilePath }) } }) downloadTask.onProgressUpdate((res) => { console.log('下载进度', res.progress) console.log('已经下载的数据长度', res.totalBytesWritten) console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite) }) downloadTask.abort() // 取消下载任务
属性同上传
websocket
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。
一个微信小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。
wx.connectSocket 创建一个socket连接
wx.connectSocket({ url: 'test.php', data:{ x: '', y: '' }, header:{ 'content-type': 'application/json' }, protocols: ['protocol1'], method:"GET" })
url 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名
header 请求头
protocols 子协议数组
method get请求
wx.onSocketOpen
监听WebSocket连接打开事件。wx.connectSocket({ url: 'test.php' }) wx.onSocketOpen(function(res) { console.log('WebSocket连接 4000 已打开!') })
wx.onSocketError
监听WebSocket错误。wx.connectSocket({ url: 'test.php' }) wx.onSocketOpen(function(res){ console.log('WebSocket连接已打开!') }) wx.onSocketError(function(res){ console.log('WebSocket连接打开失败,请检查!') })
wx.sendSocketMessage
通过 WebSocket 连接发送数据,需要先wx.connectSocket,并在
wx.onSocketOpen 回调之后才能发送。
var socketOpen = false var socketMsgQueue = [] wx.connectSocket({ url: 'test.php' }) wx.onSocketOpen(function(res) { socketOpen = true for (var i = 0; i < socketMsgQueue.length; i++){ sendSocketMessage(socketMsgQueue[i]) } socketMsgQueue = [] }) function sendSocketMessage(msg) { if (socketOpen) { wx.sendSocketMessage({ data:msg }) } else { socketMsgQueue.push(msg) } }
wx.onSocketMessage
wx.connectSocket({ url: 'test.php' }) wx.onSocketMessage(function(res) { console.log('收到服务器内容:' + res.data) })
wx.closeSocket
wx.onSocketClose
监听WebSocket关闭wx.connectSocket({ url: 'test.php' }) //注意这里有时序问题, //如果 wx.connectSocket 还没回调 wx.onSocketOpen,而先调用 wx.closeSocket,那么就做不到关闭 WebSocket 的目的。 //必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。 wx.onSocketOpen(function() { wx.closeSocket() }) wx.onSocketClose(function(res) { console.log('WebSocket 已关闭!') })
相关文章推荐
- 第三篇、微信小程序-网络请求API
- 微信小程序 网络API Websocket详解
- 微信小程序API之网络(二)上传数据
- 微信小程序API之网络(三)下载数据
- 微信小程序 网络请求API详解
- 微信小程序API之网络(一)发起请求
- 微信小程序API之网络(四)WebSocket
- 微信小程序请求网络API 400错误的解决方法
- 微信小程序 网络API 上传、下载详解
- 微信小程序把玩 Record API
- 微信小程序之文件类API
- 微信小程序网络请求的封装与填坑之路
- 微信小程序网络封装(简单高效)
- 微信小程序8 界面api
- 微信小程序位置API
- 微信小程序入门二: 条件、遍历、网络请求、获取本地图片
- 微信小程序-项目案例 (三)全局变量及封装网络请求
- 截取程序的网络封包(Delphi Hook API)
- 微信小程序循环请求api