zoukankan      html  css  js  c++  java
  • 解决JS在url中传递参数时参数包含中文乱码的问题

    需要经过两次encodeURI()编码和两次decodeURI()解码,

    使用encodeURI()编码时,

    var searchType = $("#type_select option:selected").val();//"基地动态"
        var searchContent = $("#search_val").val();//"aaaa"
        var url = encodeURI("TextSearchDetail.aspx?searchType=" + searchType + "&Content=" + searchContent);
        var enurl = encodeURI(url);//使用了两次encodeRUI进行编码
        window.location.href = enurl;

    使用decodeURI()解码时,

    $(function () {
        var postData = GetRequest();
        console.log(postData);
        console.log(postData.searchType);
        console.log(postData.Content);
    })
    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;
        }
    }

    输出为:

    Object {searchType: "基地动态", Content: "aaaa"}
    

      

  • 相关阅读:
    sql总结
    2018年6月10日笔记
    Docker入门之zabbix-agent篇
    2018年6月7日笔记
    2018年6月5日笔记
    Docker入门之container篇
    Docker入门之image篇
    Docker 入门
    2018年5月31日笔记
    2018年5月29日笔记
  • 原文地址:https://www.cnblogs.com/xiaodongaini/p/5031166.html
Copyright © 2011-2022 走看看