Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-从window.location.href查询参数_Javascript_String_Parameters - Fatal编程技术网

Javascript-从window.location.href查询参数

Javascript-从window.location.href查询参数,javascript,string,parameters,Javascript,String,Parameters,我不熟悉Javascript语法;抱歉,如果这太基本了 使用此查询: const params = queryString.parse(window.location.href) 我想: {http://localhost:3000/#access_token: "accessToken", refresh_token: "refreshToken"} 现在我可以很容易地做到: const refresh_token = params.refresh_token; 但是如何获取access

我不熟悉Javascript语法;抱歉,如果这太基本了

使用此查询:

const params = queryString.parse(window.location.href)
我想:

{http://localhost:3000/#access_token: "accessToken", refresh_token: "refreshToken"}
现在我可以很容易地做到:

const refresh_token = params.refresh_token;

但是如何获取accessToken呢?

看起来哈希是格式错误的JSON。虽然可以通过在左侧添加{,在a:,和JSON.parse-ing之前的单词字符周围添加s来解析它:

//const{hash}=window.location; //const badJSON='{'+hash.slice1; //现在,您将拥有: const badJSON='{'+'访问令牌:访问令牌,刷新令牌:刷新令牌}'; const json=badJSON.replace/\w+?=:/g,“$&”; const obj=JSON.parsejson; console.logobj.access_令牌; console.logobj.refresh_token;这就成功了:

getHashParams() {
    const hashParams = {};
    const r = /([^&;=]+)=?([^&;]*)/g;
    const q = window.location.hash.substring(1);
    let e = r.exec(q);
    while (e) {
      hashParams[e[1]] = decodeURIComponent(e[2]);
      e = r.exec(q);
    }
    return hashParams;
  }
这样称呼:

const params = this.getHashParams();
console.log(params);
记录的:

{access_token: "aceessToken", refresh_token: "refreshToken"} 

什么是你的查询字符串?字符串非常奇怪,考虑将它们固定到标准格式,这样它们就可以通过URLSARCHAPARM进行解析,我会得到QuestRisting。PARSEWOWDO.Loop.String,但是这是一个服务器到前端操作,而HREF就是我能从后端服务器获取的所有东西。构造该URL,以便它为您提供标准搜索参数。没有正则表达式或子字符串可以保存我吗?