如何编码的JSON发送到新的页面,并对其进行解码?

问题描述 投票:-1回答:1

可以说我有一个JSON {"ID":"1","Name":"XYZ"}我想编码这个数据并将其发送到新的页面说new.html和显示此数据为ID: 1 Name: XYZ。我怎样才能做到这一点?

到现在我已经试过这样:

url = 'new.html?' + encodeURIComponent(window.btoa(JSON.stringify(str)));
document.location.href = url;

此代码是我first.html脚本标签。在我new.html我尝试这样做:

<div id='here'></div>
        <script>
            window.onload = function () {
                var url = document.location.href,
                    params = url.split('?')[1].split('&'),
                    data = {}, tmp;
                    console.log(JSON.parse(window.atob(params)));
                for (var i = 0, l = params.length; i < l; i++) {
                    tmp = params[i].split('=');
                    data[tmp[0]] = tmp[1];
                }
                document.getElementById('here').innerHTML = data.id;
            }    
        </script>

但是,在执行console.log我得到一个错误:Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

javascript jquery
1个回答
1
投票

未能执行“ATOB”上“窗口”:将要被解码的字符串的编码不正确

看看你是如何编码它:

encodeURIComponent

你必须扭转,随着decodeURIComponent

© www.soinside.com 2019 - 2024. All rights reserved.