Qs.stringify、Qs.parse、JSON.stringify、JSON.parse和$.param的使用

Qs

1.vue的axios使用post时必须使用qs.stringify,而get不用
2.Qs是axios里面自带的,所以直接引入就可以了
3post请求参数是以键值对的形式存在请求体里,用Qs.stringify()就是把传入的对象转换为键值对
4.(application/x-www-form-urlencoded)
详情可以见https://blog.csdn.net/weixin_44518466/article/details/107021503

1.Qs.parse

const Qs = require('qs');
let url = 'Document=123&userId=85';
Qs.parse(url);
console.log(Qs.parse(url));
//结果:
//{
//	Document:123,
//	userId:85
//}

2.Qs.stringify

const Qs = require('qs');
let obj= {
     Document:123,
     userId:85
   };
Qs.stringify(obj);
console.log(Qs.stringify(obj));
//结果
//'Document=123&userId=85'

JSON
1.JSON.parse

var str = '{"Document":"123","userId":"85"}'
JSON.parse(str) 
//结果:
//Document: "123"
//userId: "85" 
//__proto__: Object

2.JSON.stringify

var str ='{"Document":"123","userId":"85"}'
JSON.stringify(str);
//结果:
//"{"Document":"123","userId":"85"}"
//如果对应的值为null,则会导致参数消失。

3.$.param

var str =  '{"Document":"123","userId":"85"}'
$.param(str)
//结果:
//Document=123&userId=85
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化精简这段代码// pages/Stores/Stores.js // 请求封装 import request from '../../utils/request' // 高德地图 import amapFile from '../../libs/amap-wx.130' // 地图实例 var myAmapFun Page({ data: { map: [] }, onLoad(options) { console.log(options) const that = this wx.createWACode({ path: '/pages/index/index', width: 430, scene: 'custom_value' }).then(res => { // 在页面中显示生成的小程序码 this.setData({ qrcodeUrl: res.path }); }).catch(err => { console.error(err); }); myAmapFun = new amapFile.AMapWX({ key: '5409c5fd8a9d2c7dfecef1faa8cd3ffc' }); wx.getLocation({ type: 'wgs84', isHighAccuracy: true, success(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, speed: res.speed, accuracy: res.accuracy }) request( '/stores/stores', {}, 'POST').then(res => { that.setData({ map: res.data, }) that.calculateDistance() }) } }) }, // 计算附近门店距离 calculateDistance() { const that = this; const mapVar = JSON.parse(JSON.stringify(that.data.map)); const promises = []; mapVar.forEach(item => { const origin = that.data.longitude + ',' + that.data.latitude; const destination = item.longitude + ',' + item.latitude; const promise = new Promise((resolve, reject) => { myAmapFun.getDrivingRoute({ origin: origin, destination: destination, success: function (data) { item.distance = Math.round(data.paths[0].distance / 100) / 10; resolve(); }, fail: function (err) { reject(err); } }); }); promises.push(promise); }); Promise.all(promises).then(() => { that.setData({ map: mapVar }); console.log(that.data.map) }).catch(err => { console.error(err); }); }, onShareAppMessage() { return { title: '分享标题', path: '/pages/index/index?custom_param=custom_value' }; } })
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值