5

I am new to both Ajax and Node.js + Express. At this point I am trying to figure out how to communicate with both the front and back end through buttons.

I have a button on an HTML page which I would like to use to call a function from the backend and output text to the client.

Here is what I've pieced together for what I need but I am looking for an example on how this could be done.

This is all happening on /page

index.hjs file

<button class="btn btn-success" onclick="install()">Install</button>

// Client Side Ajax Script
<script>
    $('button').click(function () {
        $.post('/page', {data: 'blah'}, function (data) {
        console.log(data);
      });
    }, 'json');
</script>

app.js file

app.post('/page', function (req, res, next) {
  calling.aFunction();
  res.write('A message!');
});

Are these all the parts that I need and what needs to be edited to get this functionality to work?

6
  • Yeah I've tried it and received the error Uncaught ReferenceError: install is not defined Commented Nov 26, 2014 at 20:28
  • That is beacuse your onclick attribute in the button element has some method install() listed you don't seem to need this since you assign a click event handler using jQuery Commented Nov 26, 2014 at 20:30
  • Removing the install() method/function doesn't seem to fix the issue. The error is gone now but not getting any output or anything. Commented Nov 26, 2014 at 20:41
  • I don't know what issue you are reffering to, you only asked if this is how you do it. Do you have an error that should be included? Have you checked your network monitor to see if the POST method is completing? Commented Nov 26, 2014 at 20:42
  • Verify in your console the request goes thru - check your node console for errors. Commented Nov 26, 2014 at 20:44

1 Answer 1

7

index.js

<button class="btn btn-success">Install</button>

// Client Side Ajax Script
<script>
    $('button').click(function () {
        $.post('/page', {data: 'blah'}, function (data) {
        console.log(data);
      });
    }, 'json');
</script>

app.js

app.post('/page', function (req, res) {
    calling.aFunction();
    res.send('A message!');
});

You should see "A message!" in the browser console.

Sign up to request clarification or add additional context in comments.

5 Comments

And how do I get the 'blah' data in app.js?
Not sure what you mean by the "blah" data. That's just my way of showing the payload being sent to the post destination
Sorry, I had to be more specific in my question: If I would like to also pass back data from index.js to the backend (app.js) with the $.post method how would I do that? Isn't that {data: 'blah'} ? And in app.post(... how would I read it, from req?
req.body.data would be "blah"
Btw, my last comment assumes you are using express-bodyparser to process POST data

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.