當在小程序中通過 url 向 <web-view> 內嵌的 H5 傳參時,當包含特殊字符時需要進行編碼處理(不然 <web-view> 中是拿不到值的,小程序竟然沒有錯誤提示...):
1、test.wxml
<view>
<web-view src="{{url}}"></web-view>
</view>
2、test.js,對參數進行編碼處理:
Page({
/**
* 頁面的初始數據
*/
data: {
url: 'https://xxx.xx.com'
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function(option) {
let loginName = encodeURI(wx.getStorageSync('loginName'));
try {
//相關操作
}
} catch (e) {
wx.navigateTo({
url: '../login/login'
})
}
let pageUrl = encodeURI(option.url);
this.setData({
url: `${this.data.url}${pageUrl}loginName=${loginName}`
});
},
onReady: function() {
},
onShow: function(option) {
}
})
3、H5 端獲取參數時需要進行解碼處理。
//獲取地址欄參數
getQueryString: (name) => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
let r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(decodeURI(r[2]));
return null;
}
}
