zoukankan      html  css  js  c++  java
  • URL带参数json传递进行解析

    注意参数格式是要加密的:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
        <title>URL带参数json传递进行解析</title>
        </head>
        <body>
        <script>
          var common = {
              getQueryString : function(name){
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
                var r = '';
                try{
                    r = location.href.split('?')[1].match(reg);
                }catch(err){}
                if (r != null && r != 'undefined') {
                    return unescape(r[2]);
                }
                return null;
              }
          }
          // 这是url上的   ?userpar=token%3dxxxx%26identity%3d123%26sysrole%3d1%26showMenu%3d0%2c1%2c2%2c3%26showColumn%3d0%2c1
          var userpar = common.getQueryString('userpar');
          console.log('参数:'+userpar)
          //这是解析后的  token=xxxx&identity=123&sysrole=1&showMenu=0,1,2,3&showColumn=0,1
          console.log("userpar里的值:    "+userpar)
    
          var newArr = userpar.split('&');
          //["token=xxxx", "identity=123", "sysrole=1", "showMenu=0,1,2,3", "showColumn=0,1"]
          console.log(newArr)
          // 数据整理
          var params = [];
          var object = {}
          for(var i in newArr){
            var obj = {}
            key = newArr[i].split('=')[0];
            value = newArr[i].split('=')[1];
            obj[key] = value;
            object =  Object.assign(object,obj); //追加json
          }
          // 进行取值
          // {token: "xxxx", identity: "123", sysrole: "1", showMenu: "0,1,2,3", showColumn: "0,1"}
          console.log(object)
        </script>
    </body>
    </html>

    在VUE中当用到split() 会提示split  没定义,解决办法,在split()前加上toString()即可:

      var newArr = userpar.toString().split('&');
  • 相关阅读:
    linux 下查看文件个数及大小
    weblogic日志小结
    Excel数据通过plsql导入到Oracle
    Linux查看外网IP
    linux挂载/卸载优盘
    git版本回退
    linux修改文件所属用户、用户组
    retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140))
    Hadoop切换namenode为active
    Netty使用LineBasedFrameDecoder解决TCP粘包/拆包
  • 原文地址:https://www.cnblogs.com/liubingyjui/p/13201372.html
Copyright © 2011-2022 走看看