0

The url contains all the query strings after the # key http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer

how do we access the params after #

1
  • 3
    The correct way of sending parameters via query string is using ? instead of # Commented Dec 7, 2018 at 4:23

4 Answers 4

1
 var url = 'http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer';

 function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\#&]' + name + '=([^&#]*)');
    var results = regex.exec(url);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, '    '));
};

 console.log(getUrlParameter('access_token'));
 console.log(getUrlParameter('expires_in'));
 console.log(getUrlParameter('token_type'));
Sign up to request clarification or add additional context in comments.

Comments

1

Best practice is to use ? instead of #

So your url should be

http://localhost:3002/callback?access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer

Now you can get the query params with below method

var express = require('express');
var app = express();

app.get('/callback', function(req, res){
  console.log('access_token: ' + req.query.access_token);
  console.log('expires_in: ' + req.query.expires_in);
  console.log('token_type: ' + req.query. token_type);
});

app.listen(3000);

Comments

0

Anything after the # isn't sent to the server by the browser.. you can
only parse it if the url is generated or obtained from the server. Then you can use nodes in-built url module to parse symbols in the url

2 Comments

is there any other way to access those query string ?
yes..don't send the token using the hash # use query params symbol ? just as @iagowp mentioned
0

You can use the substring() method: EDIT: the string you can get from response.body. You have to use body-parser or express.json

let str = "http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer";
    let index=str.indexOf("#");
    let res = str.substring(index+1);

Output: $ node server.js access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.