微信小程序带cookie的request请求可,以使服务端知道是同一个客户端请求. session_id会不变,从而很好的使用服务端的session.

写一个工具函数,直接导入使用即可,接口同 wx.request 。会自动设置和更新 cookie。

  1. const request = function (obj) {
  2. //设置cookie缓存
  3. if(obj.fail){
  4. obj.fail = function(err){
  5. wx.setStorageSync('cookie', err.header['Set-Cookie']);
  6. obj.fail(err);
  7. };
  8. }
  9. else{
  10. obj.fail = function (err) {
  11. wx.setStorageSync('cookie', err.header['Set-Cookie']);
  12. };
  13. }
  14. if(obj.success){
  15. obj.success = function (res) {
  16. wx.setStorageSync('cookie', res.header['Set-Cookie']);
  17. obj.success(res);
  18. };
  19. }
  20. else{
  21. obj.success = function (res) {
  22. wx.setStorageSync('cookie', res.header['Set-Cookie']);
  23. };
  24. }
  25.  
  26. //设置请求头
  27. if(obj.header){
  28. obj.header = {
  29. 'Cookie': wx.getStorageSync('cookie'),
  30. "Content-Type": "application/x-www-form-urlencoded",
  31. ...obj.header
  32. };
  33. }
  34. else{
  35. obj.header = {
  36. 'Cookie': wx.getStorageSync('cookie'),
  37. "Content-Type": "application/x-www-form-urlencoded",
  38. };
  39. }
  40.  
  41. wx.request(obj);
  42. };
  43.  
  44. module.exports = {
  45. request: request
  46. };

下面是我自己封装的 util.js :

  1. const Promise = require('./es6-promise.js');
  2.  
  3. //封装Request请求方法
  4. function requst(url, method, data = {}) {
  5. const app = getApp();
  6. if (app.globalData.token)
  7. {
  8. //console.log("token有值:" + app.globalData.token);
  9. data.token = app.globalData.token;
  10. }
  11.  
  12. wx.showNavigationBarLoading()
  13. data.method = method
  14. return new Promise((resove, reject) => {
  15. wx.request({
  16. url: url,
  17. data: data,
  18. header: {
  19. 'Cookie': wx.getStorageSync('cookie'),
  20. "Content-Type": "application/x-www-form-urlencoded",
  21. },
  22. method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  23. success: function (res) {
  24. wx.hideNavigationBarLoading()
  25. wx.setStorageSync('cookie', res.header['Set-Cookie']);
  26. resove(res.data)
  27. },
  28. fail: function (msg) {
  29. console.log('reqest error', msg)
  30. wx.hideNavigationBarLoading()
  31. wx.setStorageSync('cookie', res.header['Set-Cookie']);
  32. reject('fail')
  33. }
  34. })
  35. })
  36. }
  37.  
  38. module.exports = {
  39. requst: requst,
  40.  
  41. }

使用:

  1. var util = require("../../../utils/util");//工具类
  2.  
  3. /**
  4. * 获得收藏商家列表
  5. */
  6. getFavoriteShop: function (e) {
  7. wx.showToast({ title: '加载中...', icon: 'loading' });//显示加载框
  8. let that = this;
  9. let url = host + "api/prints/user/favoriteshoplist";
  10. let method = method;
  11. let data = {
  12. "user_id": user_id,
  13. "page": this.data.page,
  14. "size": this.data.size,
  15. }
  16. let result = util.requst(url, method, data);
  17. result.then(function (res) {
  18. wx.hideToast();//隐藏加载框
  19. console.log(res);
  20. if (res.code == 0) {
  21. let shop_list = res.data;
  22. //数组合并
  23. that.data.shop_list = that.data.shop_list.concat(shop_list);
  24. that.data.page++;
  25. that.setData({ shop_list: that.data.shop_list, page: that.data.page });
  26. }
  27. else {
  28. wx.showToast({ title: res.msg, icon: 'none', duration: 1000, mask: true });
  29. return false;
  30. }
  31.  
  32. }).catch(function (res) {
  33. wx.hideToast();//隐藏加载框
  34. //console.log(res);
  35. });
  36. },

转: https://www.jianshu.com/p/b4740ac347cd

微信小程序带cookie的request请求代码封装(小程序使用session)的更多相关文章

  1. 微信小程序request请求的封装

    目录 1,前言 2,实现思路 3,实现过程 3.1,request的封装 3.2,api的封装 4,实际使用 1,前言 在开发微信小程序的过程中,避免不了和服务端请求数据,微信小程序给我们提供了wx. ...

  2. 微信小程序request请求封装,验签

    1/ 公共文件util添加 request请求 //简单封装请求 function request(params, path, isShowLoading = true, goBack = false ...

  3. 微信小程序wx.request请求用POST后台得不到传递数据

    微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({              url: 'url' ...

  4. 微信小程序request请求动态获取数据

    微信小程序开发文档链接 1 后台代码: clickButton:function(){ var that = this; wx.request({ url: 'http://localhost:909 ...

  5. 关于微信小程序的Request请求错误处理

    在学微信小程序的request请求的时候,一开始报“不在以下合法域名列表中,请参考文”的错误,后来又莫名其妙的报“400 Bad Request”错误,经过半天的研究,终于搞定了,把遇到的错误给大家分 ...

  6. 微信小程序:使用wx.request()请求后台接收不到参数

    问题描述: 微信小程序:wx.request()请求后台接收不到参数,我通过wx.request()使用POST方式调用请求,参数传递不到后台 解决方案: Content-Type': 'applic ...

  7. 监控微信小程序wx.request请求失败

    在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求.其重要性不言而喻.然而,却经常遇到请求失败的问题,笔者特意谷歌"wx.request ...

  8. 微信小程序- wx.request请求不到数据

    小程序官方文档手册 https://mp.weixin.qq.com/debug/wxadoc/dev/ 小程序开发问答社区 http://www.henkuai.com/forum.php wx.r ...

  9. 微信小程序设置全局请求URL 封装wx.request请求

    app.js: App({ //设置全局请求URL globalData:{ URL: 'https://www.oyhdo.com', }, /** * 封装wx.request请求 * metho ...

随机推荐

  1. 【DRF框架】认证组件

    DRF框架的认证组件 核心代码:       self.perform_authentication(request)  框架自带模块:    from rest_framework import a ...

  2. Linux 曝出严重安全漏洞,受限用户亦可提权至 Root 身份运行任意命令!(内附解决方案)

    本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...

  3. APC (Asynchronous Procedure Call)

    系统创建新线程时,会同时创建与这个线程相关联的队列,即异步过程调用(APC)的队列. 一些异步操作可以通过加入APC来实现,比如我现在学习的IO请求/完成. BOOL ReadFileEx( HAND ...

  4. python 字符串操作list[::-1]的几种用法

    1.list[-1],此时只有一个参数,作用是通过下标访问数据,-1是倒数第一个. 2.list[:-1],作用是返回从start_index = 0到end_index = -1的一串数据这里的li ...

  5. 神经网络(9)--如何求参数: backpropagation algorithm(反向传播算法)

    Backpropagation algorithm(反向传播算法) Θij(l) is a real number. Forward propagation 上图是给出一个training examp ...

  6. 完成一个springboot项目的完整总结-------二

    我们接着上篇继续写,继续进行springboot项目 一. swagger2 接口描述,测试每个接口是否有效 1. 添加依赖 pom.xml 在编辑pom.xml之前,要先关闭spring-boot项 ...

  7. LightOJ - 1095 - Arrange the Numbers(错排)

    链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...

  8. linux 登录后有时候会出现-bash-4.1$

    转载自https://blog.csdn.net/jiedao_liyk/article/details/78470498 linux登录后有时候会出现-bash-4.1$ 造成这样的原因: 与这个用 ...

  9. 本地存储API

    一.定义 随着互联网的快速发展,基于网页的应用越来越普遍,同时也变得越来越复杂,为了满足各种各样的需求,会经常在本地存储大量的数据,HTML5规范提出了相关解决方案 本地存储设置读取方便,容量较大,s ...

  10. 历史相关API

    一.history对象 ①history.back()移动到上一个访问页面,等同于浏览器的后退键. ②history.forward()动到下一个访问页面,等同于浏览器的前进键. ③history.g ...