微信小程序带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)的更多相关文章

  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. 安装folly库以及folly的ConcurrentHashMap的简单使用

    我在写grpc的实例时, 需要使用一个多线程的hash map, C++标准库中没有多线程的hash map, facebook开源的folly中存在大量的基础类, 中间存在一个高性能的hash ma ...

  2. flask-sqlalchemy用法详解

    一. 安装 $ pip install flask-sqlalchemy 二. 配置 配置选项列表 : 选项 说明 SQLALCHEMY_DATABASE_URI 用于连接的数据库 URI .例如:s ...

  3. [极客-Linux] 05 系统调用

    分配内存的系统调用 1)brk  小内存 2)mmap  大内存 sigaction 注册信号处理函数 进程间通信: 1)消息队列,在内核中,msgget 创建 2)共享内存,shmget 内核源代码 ...

  4. Python日记(二):Python之禅

    The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Si ...

  5. zabbix Server 4.0 报警(Action)篇

    zabbix Server 4.0 报警(Action)篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看zabbix默认的Actions 1>.点击默认的Actio ...

  6. 基于yum的方式安装Cloudera Manager Server(使用Mysql 8.0版本)

    基于yum的方式安装Cloudera Manager Server(使用Mysql 8.0版本) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装和配置元数据库 1>. ...

  7. HTML&CSS基础-属性选择器

    HTML&CSS基础-属性选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html> & ...

  8. 学会github管理项目与VS单元测试——第二次作业

    GIT地址 https://github.com/Bing-Fairy/AchaoCalculator GIT用户名 Bing—Fairy 学号后五位 62502 博客地址 https://home. ...

  9. 关于List集合中元素排序问题

    问题描述: 有一个list集合,其中元素是Student对象,根据student的age排序. Student对象 /** * description * * @author 70KG * @date ...

  10. 完成一个springboot项目的完整总结------三

    这一次的总结是最关键的部分,主要涉及了ORM的三种操作,这些操作是项目的难点,三种操作包括多对一.多对多.一对多三种模式,接下来展示项目代码 1.多对一 clazz表对应grade表和charge表 ...