zoukankan      html  css  js  c++  java
  • get传中文参数乱码解决方法

      通常我们前端不同页面之间传参数用得最多的方法就是get方法:在url后面加上参数。例如:www.test.com?id=1&name=hello。

      英文和字母很好处理,但是如果有的参数值为中文呢?

      www.test.com?type='家具' ---->

      type=%E5%AE%B6%E5%85%B7,需要对得到的参数decodeURI()。为了对get过来的所有参数统一处理,写了一个方法。

      

    //解析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;
    }

    使用方法:

    var request = new getRequest();

    request.参数名……

      

      

  • 相关阅读:
    shell基础
    函数属性
    this的使用
    循环
    正则表达式中的方法
    判断是不是数组
    ECMAScript5中数组方法
    ECMAScript3中数组方法
    break和continue、return的区别
    用来枚举属性的对象工具函数
  • 原文地址:https://www.cnblogs.com/huangxi/p/5912944.html
Copyright © 2011-2022 走看看