微信小程序带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. linux下载并安装redis

    1 到http://redis.io/download 官网下下载最新稳定的redis 2 下载的redis安装包移动到要安装的位置 3 解压安装包 tar zxvf redis-3.0.5.tar. ...

  2. HDFS重启集群导致数据损坏,使用fsck命令修复过程

    HDFS重启集群导致数据损坏,使用fsck命令修复过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们先看一组输出 [root@flume112 ~]# hdfs fsck / ...

  3. 探索JVM底层奥秘ClassLoader源码分析

    1.JVM基本结构: *.java--------javac编译------>*.class-----ClassLoad加载---->运行时数据区------->执行引擎,接口库-- ...

  4. sqlite3入门之sqlite3_get_table,sqlite3_free_table

    sqlite3_get_table sqlite3_get_table函数原型: int sqlite3_get_table( sqlite3 *db, /* An open database */ ...

  5. Spring boot集成Websocket,前端监听心跳实现

    第一:引入jar 由于项目是springboot的项目所以我这边简单的应用了springboot自带的socket jar <dependency> <groupId>org. ...

  6. rest-framework认证、权限组件

    认证组件: models class User(models.Model): username = models.CharField(max_length=32) password = models. ...

  7. 用指针形式实现strstr函数

    char * mystrstr(char *dest,char * src){ char *p=null; char * temp=src; while(*dest)//只要不为'\0'就行 { p= ...

  8. Vue当中的this

    10事件绑定 methods当中的this就是Vue实例对象vm var vm = new Vue({ el: '#app', data: { num: 0 }, // 注意点: 这里不要忘记加逗号 ...

  9. Linux本地内核提权CVE-2019-13272

    简介:当调用PTRACE_TRACEME时,ptrace_link函数将获得对父进程凭据的RCU引用,然后将该指针指向get_cred函数.但是,对象struct cred的生存周期规则不允许无条件地 ...

  10. Tensorflow细节-P170-图像数据预处理

    由于6.5中提出的TFRecord非常复杂,可扩展性差,所以本节换一种方式 import tensorflow as tf from tensorflow.examples.tutorials.mni ...