微信小程序服务器开小差了,微信小程序wx.request请求封装
微信小程序 wx.request
RequestTask wx.request(Object object)发起 HTTPS 网络请求。
示例代码
wx.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})
wx.request 不进行二次封装确实不太好用 分享下我这边wx.request的封装
api.js
const app = getApp()
const request = (url, options) => {
return new Promise((resolve, reject) => {
wx.request({
url: `${app.globalData.host}${url}`,
method: options.method,
data: options.data,
header: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': app.globalData.accessToken
},
timeout: 10000,
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else if (request.statusCode === 401) {
wx.navigateTo({
url: '/pages/login/login'
})
reject(request.data)
} else if (request.statusCode === 500) {
wx.showModal({
title: '系统错误',
content: request.data.errmsg,
})
reject(request.data)
} else {
reject(request.data)
}
},
fail(error) {
wx.showModal({
title: '系统错误',
content: '服务器正在开小差',
})
reject(error.data)
},
complete: function (res) {}
})
})
}
// 把需要Loading的方法进行封装,减少不必要代码
const requestWithLoading = (url, options) => {
return new Promise((resolve, reject) => {
wx.showLoading({
title: '加载中',
})
wx.request({
url: `${app.globalData.host}${url}`,
method: options.method,
data: options.data,
header: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': app.globalData.accessToken
},
timeout: 10000,
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else if (request.statusCode === 401) {
wx.navigateTo({
url: '/pages/login/login'
})
reject(request.data)
} else if (request.statusCode === 500) {
wx.showModal({
title: '系统错误',
content: request.data.errmsg,
})
reject(request.data)
} else {
reject(request.data)
}
},
fail(error) {
wx.showModal({
title: '系统错误',
content: '服务器正在开小差',
})
reject(error.data)
},
complete: function (res) {
wx.hideLoading()
}
})
})
}
const get = (url, options = {}) => {
return request(url, {
method: 'GET',
data: options
})
}
const getWithLoading = (url, options = {}) => {
return requestWithLoading(url, {
method: 'GET',
data: options
})
}
const post = (url, options) => {
return request(url, {
method: 'POST',
data: options
})
}
const postWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'POST',
data: options
})
}
const put = (url, options) => {
return request(url, {
method: 'PUT',
data: options
})
}
const putWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'PUT',
data: options
})
}
// 不能声明DELETE(关键字)
const remove = (url, options) => {
return request(url, {
method: 'DELETE',
data: options
})
}
const removeWithLoading = (url, options) => {
return requestWithLoading(url, {
method: 'DELETE',
data: options
})
}
module.exports = {
get,
getWithLoading,
post,
postWithLoading,
put,
putWithLoading,
remove,
removeWithLoading
}
使用方式
const api = require('../../api/api')
Page()前引入
api.post(login, {
data: ''
}).then(res => {
if(){}
}).catch(err => {
wx.showToast({
title: err.message,
icon: 'none'
})
})
data 参数说明
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
对于 GET 方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)
对于 POST 方法且 header[‘content-type’] 为 application/json 的数据,会对数据进行 JSON 序列化
对于 POST 方法且 header[‘content-type’] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)
微信小程序服务器开小差了,微信小程序wx.request请求封装相关推荐
- 微信小程序wx.request请求封装,和跨域的解决。
建议把所有请求抽离到不同页面对应的js文件中,可以方便后期的修改和排查问题,小程序请求是通过微信后台来请求我们的后台地址来进行后端映射.你请求的接口实际到微信的后端做了一道映射,微信后端拿到你的 wx ...
- 微信小程序请求php文件报错,微信小程序wx.request请求数据报错
首先写一个后台的数据接口,地址是:http://localhost/weicms/index... 然后使用wx.request调用后台数据接口的地址 示例代码1 wx.request({ 2 url ...
- 微信小程序wx.request请求
微信小程序网络请求wx.request请求 wx.request是向指定域名发送http请求,并且需要在微信小程序管理后台中加入指定域名(微信小程序后台中只能添加https的域名,可能是微信官方觉得这 ...
- 微信小程序网络请求异常怎么办_监控微信小程序wx.request请求失败
在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求.其重要性不言而喻.然而,却经常遇到请求失败的问题,笔者特意谷歌"wx.request ...
- 微信小程序 wx.request 的封装
自学转行到前端也已近两年,也算是简书和掘金的忠实粉丝,但是以前一直惜字如金(实在是胆子小,水平又低),现在我决定视金钱如粪土(就只是脸皮厚了,水平就那样),好了废话不多说,切入主题,最近自己尝试了一下 ...
- 微信小程序请求webapi调试抓包工具Fiddler以及wx.request请求webapi时web服务器端解析不到code问题
参考链接 https://blog.csdn.net/weixin_40188140/article/details/82851315 https://www.cnblogs.com/chenboyi ...
- 微信小程序wx.request请求服务器json数据并渲染到页面
微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更新,而小程序数据主要是json数据格式,所以我们可以利用php操作数据库,把数据以json格式数据输出即可.现在给大家讲一下,微信小程序的wx ...
- win7下搭建小程序服务器,重磅!微信PC端支持小程序直接开启 适配Win7及以上系统...
原标题:重磅!微信PC端支持小程序直接开启 适配Win7及以上系统 腾讯科技讯 8月9日,腾讯科技在"微信开放社区"发现,微信正在测试"PC端支持打开小程序"能 ...
- 微信小游戏服务器数据持久化,微信小程序wx.setStorage数据缓存实现缓存过期时间...
为了项目性能等方面的考虑,有时候有必要为用户经常访问的页面使用缓存机制:在技术上,服务端和前端都有相应的缓存机制.比如传统的session及cookie等等,在微信小程序中,并没有cookie机制,但 ...
最新文章
- python if break_python_if_else,while,break
- 玩转jquery插件之flexigrid 【转】
- vue 打包css路径不对_vue项目打包后css背景图路径不对的问题
- python变量使用的三个步骤_Python 动态生成多个变量
- 国科大学习资料--矩阵分析与应用(李保滨)--2017年期末考试试卷
- linux使用脚本执行vim命令行,linux – 如何通过shell脚本执行vim命令
- html返回按钮 超链接,ppt超链接返回键
- zemax中如何和matlab中通信,如何在ZEMAX及MATLAB之间通信.doc
- 基于ARM64架构飞腾2000CPU的浪潮CE3000F机器安装银河麒麟系统和Docker
- PHP 开发杂谈:对后端开发的思考
- Andorid Activity的本质是什么
- Manjaro-architect 安装指南
- 数据库与身份认证(学习笔记)
- Android肝帝战纪之字体图标库(iconify)的简单使用
- Linux服务器查看带宽,进程和线程
- 求解两圆相交的交点坐标
- 部分应用redis账号被锁,解锁
- Vue初体验(七)使用Vue实现一个简单的聊天框
- 一些金融术语(随时更新)
- ARP的欺骗泛洪攻击的防御——DAI动态ARP监控技术、
热门文章
- SharePoint技术峰会2009视频(6)
- Win XP系统的开机菜单具体含义
- mongodb 导出 带条件_将 MongoDB 导出成 csv
- win10计算机里文件夹怎么删除文件,如何删除win10“此电脑”中6个文件夹?
- 德州职业技术学院计算机系,德州职业技术学院计算机系元旦晚会相声视频
- 【修正】销售开票BAPI实例:BAPI_BILLINGDOC_CREATEMULTIPLE
- 【ABAP增强】基于BADI的增强
- 80%的人都混淆的BI和报表之不同
- 你有多了解hybris ?
- 颈部有细纹怎么缓解?