Solve the error problem: SyntaxError: Unexpected end of JSON input parameter

1. Problem

        

When jumping to a page, the path needs to bring some parameter types, query and params. When params is brought in as an object, the object must first be converted into a string type, and then converted into the required object on the corresponding page to achieve the effect. 

 A delivery page:

GetAddContainer(e){
    let money = e.currentTarget.dataset.money
    console.log(money);
    const data = JSON.stringify(money)
    wx.navigateTo({
      url: `/pages/tixian/tixian?data=` +data,
    }) 
},

 B receiving page:

onLoad(options) {
    let valOptions = JSON.parse((options.data))
    this.setData({
      tixianUserInfo:valOptions,
    })
    this.getstoreInfo()
    console.log(this.data.tixianUserInfo);
},

2. Solve

 What if the parameters passed are not included? Special characters such as & * can be passed normally. If one of the special characters is included, the above problems will occur. You can carry encodeURIComponent to solve the encoding when passing parameters, and then use decodeURIComponent to solve the encoding when accepting parameters. Parse special characters

A delivery page: 

GetAddContainer(e){
    let money = e.currentTarget.dataset.money
    console.log(money);
    const data = JSON.stringify(money)
    wx.navigateTo({
      url: `/pages/tixian/tixian?data=` + encodeURIComponent(data),
    })
},

B receiving page: 

onLoad(options) {
    let valOptions = JSON.parse(decodeURIComponent(options.data))
    this.setData({
      tixianUserInfo:valOptions,
    })
    this.getstoreInfo()
    console.log(this.data.tixianUserInfo);
 },

Success result: 

Guess you like

Origin blog.csdn.net/frelly01/article/details/132604463