javascript - How to get parameters when there is a # sign in the URL? nodejs
世界只因有你
世界只因有你 2017-05-24 11:38:59
0
3
1239

For examplehttps://beta.biaoqing.com/callback/qq/?#access_token=CF0C8D1CDFEE38425CDB8A719080A153&expires_in=7776000
I use var access_token=req.query in nodejs. access_token;Cannot obtain access_token

世界只因有你
世界只因有你

reply all(3)
仅有的幸福

Unable to obtain, # is the front-end hash. When sending a request, the content after the hash will not be sent to the URL. I don’t know you? Why is there #

at the end?
黄舟

Teach you a special method:

const querystring = require('querystring');
let str = 'https://beta.biaoqing.com/callback/qq/#access_token=CF0C8D1CDFEE38425CDB8A719080A153&expires_in=7776000';

let r = querystring.parse(str);
console.log(r);

for(value in r){
    console.log('key: ', value);
    console.log('value: ', r[value]);
}

It’s considered opportunistic.
The Object.values() method in es6 can replace the above for in method, but it is only compatible with node v7.x version.

------------------------Separator----------------------- --------

Improve the above method: remove the # symbol and it will be OK.

const querystring = require('querystring');
const url = require('url');

let str = 'https://beta.biaoqing.com/callback/qq/?#access_token=CF0C8D1CDFEE38425CDB8A719080A153&expires_in=7776000';
let str2 = str.replace(/\#/g,'');//去掉 # 符号
let obj_query = querystring.parse(url.parse(str2).query); // 解析URL的 query 部分
console.log(obj_query.access_token);

迷茫

This is a fallback landing page authorized by a third party, right?

The front end of this landing page can get the content behind the hash, so the logic can be as follows:

The user jumps from your website to third-party authorization-> The third-party authorization successfully jumps to the callback landing page preset on your website-> The callback landing page hash contains access_token and other parameters-> The landing page js is taken Send an asynchronous request to the interface set in the backend using the parameters in the hash -> ajax returns success, js controls the jump -> The browser is navigated to the real user-interactive authorization success page

In other words, the landing page returned in the first step is a transit page. The purpose is to use js to remove the hashed content and send it back to the backend with ajax. After the backend gets the token authorized by the third party, js immediately controls the jump. change. It is best to add some UI to this transfer page, such as a prompt such as jumping, which is absolutely acceptable in terms of user experience.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template