url传参时包含中文

2018-08-28  本文已影响0人  西西_80ff

在js中进行传参,由于包含中文,所以需要经过两次encodeURI()编码和两次decodeURI()解码

a.html跳转b.html传中文参数:

a.html:

html:   

 <a @click="jump(r.uid,r.name)"></a>

js:

jump:function(uid,name){

            var url=encodeURI(encodeURI("share.html?uid="+uid+"&name="+name));

            window.location.href=url;

}

b.html:

js:

function GetRequest() {

    var url =decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串,使用两次decodeRUI解码

    var theRequest = new Object();

    if (url.indexOf("?") != -1) {

        var str = url.substr(1),

        strs = str.split("&");

        for (var i = 0; i < strs.length; i++) {

            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);

        }

        return theRequest;

    }

}

var postData = GetRequest();

    console.log(postData); //postData是一个json对象:{uid:'12334',name:'我的名字'};可直接使用postData.uid;    postData.name

上一篇 下一篇

猜你喜欢

热点阅读