I am trying to create a "follow button" using jquery and node.js My front-end is like this:
<a id="author" href="#"><%= showPost.username %></a> //this displays a username
<p id="follower"><%= currentUser.username %></p> //this displays the current logged in username
<button style="width:100px;" type="button" id="fbtn">Follow</button>
jquery is like this:
$("#fbtn").click(function(){
var fbtn = $('#fbtn');
var follower_name=$('#follower').text();
var followee_name = $('#author').text();
fbtn.text('Unfollow');
$.ajax({
url: '/follow',
method: 'POST',
dataType: 'application/json',
data:JSON.stringify({follower_name:follower_name,followee_name:followee_name}),
success: function(data){
console.log("success");
console.log(data);
}
});
});
back-end is like this:
app.post("/follow", function(req, res){
var followee = req.body.followee_name;
console.log("Followed");
console.log(followee);
res.send("Worked!!!")
});
currently I am using console.log so that I can see if the data is passing or not. However, I am getting "undefined" in the console. I also got rid of req.body and I get the same log.
I am also getting this in the browser console:
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
After I get the data from jquery I will insert it to mySQL database. But I don't know how to pass the data to the back-end.