微信小程序带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. 【转】Anaconda安装与使用

    PS:这还是17年一次数据挖掘训练营使用的软件 [转至]https://blog.csdn.net/m0_37605642/article/details/98726766 安装和配置 1.在官网或清 ...

  2. CentOS7- ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705

    CentOS7重启后,xshell连接,后出现ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548 ...

  3. error 106: Can't Access ASP.NET\ClientFiles\

    Error 1606 Can’t access ASP.NET\ClientFiles\ when installing Crystal Reports Support Pack 10     Sea ...

  4. thinkphp概述

    thinkphp框架是一个免费的,开源,快速,简单的面向对象的轻量级PHP开发框架. 了解什么是thinkphp概述,thinkphp项目目录结构,thinkphp的控制器,视图,thinkphp项目 ...

  5. Surface 系统恢复

    Surface Pro 6超详细教程之下载Surface 系统恢复镜像并制作系统恢复U盘 https://www.jianshu.com/p/d1b41d913f91 怎样创建Windows 10系统 ...

  6. 阿里巴巴开源性能监控神器Arthas jvm

    原文:https://www.cnblogs.com/testfan2019/p/11038791.html 如果问性能测试中最难的是哪部分,相信很多人会说“性能调优”.确实是这样,性能调优是一个非常 ...

  7. mysql存储、function、触发器等实例

    一.创建数据库&表 DROP DATABASE IF EXISTS security; CREATE database security; USE security; CREATE TABLE ...

  8. [转]Linux网络 - 数据包的接收过程

    转, 原文: https://segmentfault.com/a/1190000008836467 ------------------------------------------------- ...

  9. JVM对象创建

    1.JVM对象创建:java程序运行过程中,无时无刻都有对象被创建出来.在语言层面上就是new关键字. 2.JVM对象创建过程: (1)JVM遇到一条new指令后,首先会去常量池中,检查这个指令的参数 ...

  10. php 文件包含函数

    在实际开发中,常常需要把程序中的公用代码放到一个文件中,使用这些代码的文件只需要包含这个文件即可.这种方法有助于提高代码的重用性,给代码的编写与维护带来很大的便利.在PHP中, 有require.re ...