美文网首页
js 页面传递参数乱码

js 页面传递参数乱码

作者: Avery_G | 来源:发表于2021-07-12 13:57 被阅读0次

js 页面传递参数如果是中文,会出现乱码,解决方法如下:

1.在传递参数的页面,使用 encodeURI 进行转码,注意需要加两层

let url = './result.html?examId=' + examId + '&phone=' + phone + '&name=' + name
url = encodeURI(url);
url = encodeURI(url);

2.在接收参数的页面,使用 decodeURI 解码

// 截取 URL 参数
function getParams(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
};
const name = decodeURI(getParams('name'))

相关文章

网友评论

      本文标题:js 页面传递参数乱码

      本文链接:https://www.haomeiwen.com/subject/aayrpltx.html