微信小程序带cookie的request请求代码封装(小程序使用session)
微信小程序带cookie的request请求可,以使服务端知道是同一个客户端请求. session_id会不变,从而很好的使用服务端的session.
写一个工具函数,直接导入使用即可,接口同 wx.request
。会自动设置和更新 cookie。
- const request = function (obj) {
- //设置cookie缓存
- if(obj.fail){
- obj.fail = function(err){
- wx.setStorageSync('cookie', err.header['Set-Cookie']);
- obj.fail(err);
- };
- }
- else{
- obj.fail = function (err) {
- wx.setStorageSync('cookie', err.header['Set-Cookie']);
- };
- }
- if(obj.success){
- obj.success = function (res) {
- wx.setStorageSync('cookie', res.header['Set-Cookie']);
- obj.success(res);
- };
- }
- else{
- obj.success = function (res) {
- wx.setStorageSync('cookie', res.header['Set-Cookie']);
- };
- }
- //设置请求头
- if(obj.header){
- obj.header = {
- 'Cookie': wx.getStorageSync('cookie'),
- "Content-Type": "application/x-www-form-urlencoded",
- ...obj.header
- };
- }
- else{
- obj.header = {
- 'Cookie': wx.getStorageSync('cookie'),
- "Content-Type": "application/x-www-form-urlencoded",
- };
- }
- wx.request(obj);
- };
- module.exports = {
- request: request
- };
下面是我自己封装的 util.js :
- const Promise = require('./es6-promise.js');
- //封装Request请求方法
- function requst(url, method, data = {}) {
- const app = getApp();
- if (app.globalData.token)
- {
- //console.log("token有值:" + app.globalData.token);
- data.token = app.globalData.token;
- }
- wx.showNavigationBarLoading()
- data.method = method
- return new Promise((resove, reject) => {
- wx.request({
- url: url,
- data: data,
- header: {
- 'Cookie': wx.getStorageSync('cookie'),
- "Content-Type": "application/x-www-form-urlencoded",
- },
- method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
- success: function (res) {
- wx.hideNavigationBarLoading()
- wx.setStorageSync('cookie', res.header['Set-Cookie']);
- resove(res.data)
- },
- fail: function (msg) {
- console.log('reqest error', msg)
- wx.hideNavigationBarLoading()
- wx.setStorageSync('cookie', res.header['Set-Cookie']);
- reject('fail')
- }
- })
- })
- }
- module.exports = {
- requst: requst,
- }
使用:
- var util = require("../../../utils/util");//工具类
- /**
- * 获得收藏商家列表
- */
- getFavoriteShop: function (e) {
- wx.showToast({ title: '加载中...', icon: 'loading' });//显示加载框
- let that = this;
- let url = host + "api/prints/user/favoriteshoplist";
- let method = method;
- let data = {
- "user_id": user_id,
- "page": this.data.page,
- "size": this.data.size,
- }
- let result = util.requst(url, method, data);
- result.then(function (res) {
- wx.hideToast();//隐藏加载框
- console.log(res);
- if (res.code == 0) {
- let shop_list = res.data;
- //数组合并
- that.data.shop_list = that.data.shop_list.concat(shop_list);
- that.data.page++;
- that.setData({ shop_list: that.data.shop_list, page: that.data.page });
- }
- else {
- wx.showToast({ title: res.msg, icon: 'none', duration: 1000, mask: true });
- return false;
- }
- }).catch(function (res) {
- wx.hideToast();//隐藏加载框
- //console.log(res);
- });
- },
转: https://www.jianshu.com/p/b4740ac347cd
微信小程序带cookie的request请求代码封装(小程序使用session)的更多相关文章
- 微信小程序request请求的封装
目录 1,前言 2,实现思路 3,实现过程 3.1,request的封装 3.2,api的封装 4,实际使用 1,前言 在开发微信小程序的过程中,避免不了和服务端请求数据,微信小程序给我们提供了wx. ...
- 微信小程序request请求封装,验签
1/ 公共文件util添加 request请求 //简单封装请求 function request(params, path, isShowLoading = true, goBack = false ...
- 微信小程序wx.request请求用POST后台得不到传递数据
微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({ url: 'url' ...
- 微信小程序request请求动态获取数据
微信小程序开发文档链接 1 后台代码: clickButton:function(){ var that = this; wx.request({ url: 'http://localhost:909 ...
- 关于微信小程序的Request请求错误处理
在学微信小程序的request请求的时候,一开始报“不在以下合法域名列表中,请参考文”的错误,后来又莫名其妙的报“400 Bad Request”错误,经过半天的研究,终于搞定了,把遇到的错误给大家分 ...
- 微信小程序:使用wx.request()请求后台接收不到参数
问题描述: 微信小程序:wx.request()请求后台接收不到参数,我通过wx.request()使用POST方式调用请求,参数传递不到后台 解决方案: Content-Type': 'applic ...
- 监控微信小程序wx.request请求失败
在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求.其重要性不言而喻.然而,却经常遇到请求失败的问题,笔者特意谷歌"wx.request ...
- 微信小程序- wx.request请求不到数据
小程序官方文档手册 https://mp.weixin.qq.com/debug/wxadoc/dev/ 小程序开发问答社区 http://www.henkuai.com/forum.php wx.r ...
- 微信小程序设置全局请求URL 封装wx.request请求
app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * metho ...
随机推荐
- 【DRF框架】认证组件
DRF框架的认证组件 核心代码: self.perform_authentication(request) 框架自带模块: from rest_framework import a ...
- Linux 曝出严重安全漏洞,受限用户亦可提权至 Root 身份运行任意命令!(内附解决方案)
本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...
- APC (Asynchronous Procedure Call)
系统创建新线程时,会同时创建与这个线程相关联的队列,即异步过程调用(APC)的队列. 一些异步操作可以通过加入APC来实现,比如我现在学习的IO请求/完成. BOOL ReadFileEx( HAND ...
- python 字符串操作list[::-1]的几种用法
1.list[-1],此时只有一个参数,作用是通过下标访问数据,-1是倒数第一个. 2.list[:-1],作用是返回从start_index = 0到end_index = -1的一串数据这里的li ...
- 神经网络(9)--如何求参数: backpropagation algorithm(反向传播算法)
Backpropagation algorithm(反向传播算法) Θij(l) is a real number. Forward propagation 上图是给出一个training examp ...
- 完成一个springboot项目的完整总结-------二
我们接着上篇继续写,继续进行springboot项目 一. swagger2 接口描述,测试每个接口是否有效 1. 添加依赖 pom.xml 在编辑pom.xml之前,要先关闭spring-boot项 ...
- LightOJ - 1095 - Arrange the Numbers(错排)
链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...
- linux 登录后有时候会出现-bash-4.1$
转载自https://blog.csdn.net/jiedao_liyk/article/details/78470498 linux登录后有时候会出现-bash-4.1$ 造成这样的原因: 与这个用 ...
- 本地存储API
一.定义 随着互联网的快速发展,基于网页的应用越来越普遍,同时也变得越来越复杂,为了满足各种各样的需求,会经常在本地存储大量的数据,HTML5规范提出了相关解决方案 本地存储设置读取方便,容量较大,s ...
- 历史相关API
一.history对象 ①history.back()移动到上一个访问页面,等同于浏览器的后退键. ②history.forward()动到下一个访问页面,等同于浏览器的前进键. ③history.g ...