解決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"}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM