Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make action on node.js server after button click using jQuery AJAX

I have a problem. I want to make server do something after clicking on button. This is my HTML code:

    <input name="like" id="like" value="Like" type="submit" />
    <script>
        $('like').click(function(){
            $.post('/test')
        });
    </script>

and this is my server-side code:

app.post('/test', function (req, res) {
    console.log('works');
});

And it doesn't work.

like image 724
kuba12 Avatar asked Jul 25 '14 12:07

kuba12


People also ask

How do I trigger a button click script?

In the Elements tray, click Form Elements and drag a Submit button onto your page. In the Button Settings tab of the Properties panel, set the button mode to Trigger. Go to the Actions and Events tab of the Properties panel, and select Add New Action. Click Done.

Can you use Ajax with node js?

Node, Ajax and JavaScript integration Save the index. html file and refresh the web browser. Uploads to Node. js will go through Ajax, and thus create a full JavaScript file uploader with JavaScript running both on the client and the server.

How can we send data to server using Ajax?

The jQuery. post( url, [data], [callback], [type] ) method loads a page from the server using a POST HTTP request. data − This optional parameter represents key/value pairs or the return value of the . serialize() function that will be sent to the server.


1 Answers

Your problem is here, you have forgotten the # for targeting element by id, so click would never be invoke.

$('#like').click(function(){
    $.post('/test');
});
like image 199
GillesC Avatar answered Oct 30 '22 06:10

GillesC