美文网首页工作生活
URL中传递参数时中文的转化

URL中传递参数时中文的转化

作者: 文朝明 | 来源:发表于2019-07-04 12:00 被阅读0次

    项目中,在页面之间点击跳转时需要进行参数传递,需要将中文的参数放入到url中进行跳转传参。

    传值

                            let SnPh = item.SnPh
                            let STr = item.STr
                            let QCc = item.QCc
                            let SnAddToo = item.SnAddToo
                            let SnEndToo = item.SnEndToo
                            let monsterSnngz = item.monsterSnngz
                            let monsterSnnfw = item.monsterSnnfw
                            let monsterSnnxz = item.monsterSnnxz
                            console.log(monsterSnnxz)
                            let url = encodeURIComponent("SnPh=" + SnPh + "&STr=" + STr + "&QCc=" + QCc + "&SnAddToo=" + SnAddToo +
                                "&SnEndToo=" + SnEndToo + "&monsterSnngz=" + monsterSnngz + "&monsterSnnfw=" + monsterSnnfw + "&monsterSnnxz=" + monsterSnnxz)
                            console.log(url)
                            let enurl = encodeURI(url);
                            console.log(enurl)
                            monsterlist += " <a  href='monsterxq.html?" + enurl + "'>"
    

    解析

     $(document).unbind() //执行后消除绑定
            // 跨域调用getJSON
            // 获取传来的值
        function GetRequest() {
            var enurl = location.search;
            //获取url中"?"符后的字串 
            fiurl = decodeURIComponent(enurl)
            url = decodeURIComponent(fiurl)
            console.log(url)
            var theRequest = new Object();
            if (url.indexOf("?") != -1) { //判断是否含有?,有则不等于-1
                var str = url.substr(1); //从url后面起,第二个字符开始开始计算
                strs = str.split("&"); //将字符串切割成字符串数组
                console.log(strs)
                for (var i = 0; i < strs.length; i++) { //theRequest[strs[i].split("=")[0]]
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1])
    
                }
                return theRequest;
            }
        }
        var theRequest = GetRequest()
        console.log(theRequest)
        console.log(theRequest.SnPh)
        let SnPh = theRequest.SnPh
        let STr = theRequest.STr
        let QCc = theRequest.QCc
        let SnAddToo = theRequest.SnAddToo
        let SnEndToo = theRequest.SnEndToo
        let monsterSnngz = theRequest.monsterSnngz
        let monsterSnnfw = theRequest.monsterSnnfw
        let monsterSnnxz = theRequest.monsterSnnxz
    

    相关文章

      网友评论

        本文标题:URL中传递参数时中文的转化

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