跳转页面传递参数

var selectWorker = JSON.stringify(selectWorker);
uni.navigateTo({
    url: '../recordForm/recordForm?selectWorker=' + selectWorker
})
onLoad(option) {
	console.log(option)
	var _this = this;

	// 选中的工人
	var selectWorker = JSON.parse(option.selectWorker);
	var workerIdsArray = [];
	for (var i = 0; i < selectWorker.length; i++) {
		workerIdsArray.push(selectWorker[i].id);
	}
	var workerIds = workerIdsArray.join(',');
	console.log(workerIds);
	_this.selectWorker = selectWorker;
	_this.workerIds = workerIds;
},

报错提示SyntaxError:Unexpected end of JSON input

 解决方案

原因:若对象的参数或数组的元素中遇到地址中包括?& - _ . ! ~ * ' ( )等特殊符号时,对象/数组先要通过JSON.stringify转化为字符串再通过encodeURIComponent编码,接收时,先通过decodeURIComponent解码再通过JSON.parse转换为JSON格式的对象/数组。

修改如下:

var selectWorker = JSON.stringify(selectWorker);
uni.navigateTo({
    url: '../recordForm/recordForm?selectWorker=' + encodeURIComponent(selectWorker)
})
onLoad(option) {
	console.log(option)
	var _this = this;

	// 选中的工人
	var selectWorker = JSON.parse(decodeURIComponent(option.selectWorker));
	var workerIdsArray = [];
	for (var i = 0; i < selectWorker.length; i++) {
		workerIdsArray.push(selectWorker[i].id);
	}
	var workerIds = workerIdsArray.join(',');
	console.log(workerIds);
	_this.selectWorker = selectWorker;
	_this.workerIds = workerIds;
},

Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐