I am using an href to link the user to a route. My problem is that I need to send a parameter to the dashboard while clicking on the link. The easiest way to do this would be to get the id or name of the href in the app.js and send it to the dashboard.
Here is my code:
//the href in the index.ejs
<a href="/dashboard" id="ANDROID">ANDROID</a>
//in the app.js
app.get('/dashboard', function(req, res) {
var url = req.url;
console.log("url: " + url); //prints out url: /dashboard
var id = req.url.id;
console.log("id: " + id); //prints out id: undefined
});
How can I grap the id or name of the href?
I am using an href to link the user to a route. My problem is that I need to send a parameter to the dashboard while clicking on the link. The easiest way to do this would be to get the id or name of the href in the app.js and send it to the dashboard.
Here is my code:
//the href in the index.ejs
<a href="/dashboard" id="ANDROID">ANDROID</a>
//in the app.js
app.get('/dashboard', function(req, res) {
var url = req.url;
console.log("url: " + url); //prints out url: /dashboard
var id = req.url.id;
console.log("id: " + id); //prints out id: undefined
});
How can I grap the id or name of the href?
Share Improve this question asked Oct 9, 2014 at 12:09 sanyoohsanyooh 1,6412 gold badges21 silver badges35 bronze badges 1- It's hard to know what you are actually trying to achieve, but at a guess I'd say that it's because req.url only returns the url and doesn't have information on the link that it came from. I'd consider argument passing, express facilitates this: expressjs./4x/api.html#app.param – NMunro Commented Oct 9, 2014 at 12:20
1 Answer
Reset to default 3If I understand correctly, you need to pass the parameter to the URL. The ID of the "a" element is only a DOM ID and it is not in any way connected to the URL the link leads to.
You could try something like this:
<a href="/dashboard?id=ANDROID" id="ANDROID">ANDROID</a>
Then in the code:
app.get('/dashboard', function(req, res) {
var id = req.query.id;
...
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335868a4623100.html
评论列表(0条)