get傳中文參數亂碼解決方法

  一般咱們前端不一樣頁面之間傳參數用得最多的方法就是get方法:在url後面加上參數。例如:www.test.com?id=1&name=hello。前端

  英文和字母很好處理,可是若是有的參數值爲中文呢?url

  www.test.com?type='傢俱' ---->spa

  type=%E5%AE%B6%E5%85%B7,須要對獲得的參數decodeURI()。爲了對get過來的全部參數統一處理,寫了一個方法。code

  

//解析URL上的參數
function getRequest() {
   var url = window.location.search; //獲取url中"?"符後的字串
   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]]=decodeURI(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}

使用方法:blog

var request = new getRequest();get

request.參數名……it

相關文章
相關標籤/搜索