方法一 url传参
// 引入 upload.html 并通过 url 传入 token 参数
const token = uni.getStorageSync('Authorization')
this.wv = plus.webview.create(`/hybrid/html/upload.html?token=${token}`, '', {
'uni-app': 'none',
top: '50%',
left: '80%',
background: 'transparent'
});
var currentWebview = this.$scope.$getAppWebview();
currentWebview.append(this.wv);
弊端:
1.参数会被抓取,如果携带敏感信息会存在安全隐患;
2.url会有长度限制,如果携带的数据过多会传不过去;
3.没有对应的webview向app传参的回传方法;
方法2 plus.webview.create(url, id, style, {data:{}})的方法携带参数,并使用uni.postMessage()的方法回传参数。
let wv = plus.webview.create(
'xxx.xxxx.xxx?',//date保证不走缓存
'batch_view',
{
top: '0',
left: '0',
height: '100%',
width: '100%'
},
{
data: {
token: token,
userInfo: userInfo,
implantType: 'uniapp'
}
}) //不用data键值对的方式传的话,h5接收后会是多个字段,而非一个对象
let currentWebview = this.$mp.page.$getAppWebview()
currentWebview.append(wv);//重要,否则会失效
html中
if (window.plus) {
plusReady();
} else {
document.addEventListener('plusready', plusReady, false);
}
function plusReady() {
const data = plus.webview.getWebviewById("batch_view").data;//上传时最后一个参数
var xhr = new XMLHttpRequest();
xhr.open('POST','url');//url你要上传的地址
xhr.send(formData);
xhr.onreadystatechange=function(){
if(xhr.readyState===4&&xhr.status===200)
{
console.log("上传成功",xhr.responseText);
//接收返回的结果并展示出来
var data = JSON.parse(xhr.responseText)
}
};
}
参考
(48条消息) uni-app和web-view页面相互传参_uniapp向webview发送数据_湫风洛夜的博客-CSDN博客