0

jQuery on click function not working to work with express to route to a handlebars page to display passed information.

I've tried changing the get to post. Not using handlebars and just having in html file and using path.sendFile or chaning res.render to be res.send

Heres the on click function

$("#song").on("click", function(){
  var songArtist = $(this).text();

  $.get("/results/:" + songArtist);
});

Heres the express route

app.get("/results/:songArtist", function(req, res) {
    res.render("songResult");
    //res.sendFile(path.join(__dirname + "./../views/result.html"));
  });

I expect that when the divs with the song id is clicked to load to a new page.

1 Answers1

0

Get rid of the : in the $.get() in the front-end JS. The : is only used on the back-end to represent variable URL parameters.

So on the back-end, when you listen for get requests at "/results/:songArtist", whatever comes after "/results/" will now be referred to in the function as req.params.songArtist.

For example, if I send a get request to "/results/pinkfloyd", inside the listener function you provided above, req.params.songArtist === pinkfloyd.

boc4life
  • 941
  • 5
  • 10