javascript - How to get parameters when there is a # sign in the URL? nodejs
世界只因有你2017-05-24 11:38:59
0
3
1170
For examplehttps://beta.biaoqing.com/callback/qq/?#access_token=CF0C8D1CDFEE38425CDB8A719080A153&expires_in=7776000 I usevar access_token=req.query in nodejs. access_token;Cannot obtain access_token
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 #
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.
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.
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:
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.
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.