当前位置:   article > 正文

解决报错SyntaxError:Unexpected end of JSON input_syntaxerror: unexpected end of json input

syntaxerror: unexpected end of json input

跳转页面传递参数

  1. var selectWorker = JSON.stringify(selectWorker);
  2. uni.navigateTo({
  3. url: '../recordForm/recordForm?selectWorker=' + selectWorker
  4. })
  1. onLoad(option) {
  2. console.log(option)
  3. var _this = this;
  4. // 选中的工人
  5. var selectWorker = JSON.parse(option.selectWorker);
  6. var workerIdsArray = [];
  7. for (var i = 0; i < selectWorker.length; i++) {
  8. workerIdsArray.push(selectWorker[i].id);
  9. }
  10. var workerIds = workerIdsArray.join(',');
  11. console.log(workerIds);
  12. _this.selectWorker = selectWorker;
  13. _this.workerIds = workerIds;
  14. },

报错提示SyntaxError:Unexpected end of JSON input

 解决方案

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

修改如下:

  1. var selectWorker = JSON.stringify(selectWorker);
  2. uni.navigateTo({
  3. url: '../recordForm/recordForm?selectWorker=' + encodeURIComponent(selectWorker)
  4. })
  1. onLoad(option) {
  2. console.log(option)
  3. var _this = this;
  4. // 选中的工人
  5. var selectWorker = JSON.parse(decodeURIComponent(option.selectWorker));
  6. var workerIdsArray = [];
  7. for (var i = 0; i < selectWorker.length; i++) {
  8. workerIdsArray.push(selectWorker[i].id);
  9. }
  10. var workerIds = workerIdsArray.join(',');
  11. console.log(workerIds);
  12. _this.selectWorker = selectWorker;
  13. _this.workerIds = workerIds;
  14. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/228979
推荐阅读
相关标签
  

闽ICP备14008679号