Take the 2-minute tour ×

I can't seem to get the AJAX call correct. There have been other QA that deal with the $.ajax() function but I'm trying to solve this with $.post().

When the form button is clicked the javascript at the head is executed, which includes a $.post(). The url /login is routed through and passed to loginPost function. There a response is determined and sent back to the javascript (right?). Instead, webpage renders the response (pass || fail).

Why isn't the response from the AJAX call being sent back to get processed?

This is a simple example that I am working with to get me better acquainted to how AJAX in expressJS and jQuery work. Any Help is greatly appreciated!


  $(document).ready(function(req, res) {
    $('#login').submit(function() {
    var formData = $(this).serialize();
    $.post('/login', formdata, processData).error('ouch');

    function processData(data, status) {
      if (data == 'pass') {
        $('#content').html('<p>You have successfully loggin in!</p>');
      } else {
        if (! $('#fail').length) {
          $('#formFrame').prepend('<p id="fail">Incorrect login information. Please try again)</p>');
    } //end processData
  }); //end submit
}); //end ready

h1= title
  form(id='login', action='/login', method='POST')
  label(for='username') Username:
  input(id='username', type='text', name='username')
  label(for='password') Password:
  input(id='button', type='submit', name='button', value='Submit')


app.post('/login', loginPost);


module.exports.loginPost = function(req, res) {
    var password = 'admin'
      , username = 'user'
      , data = req.body;

    if (data.username == username && data.password == password) {
    } else {
share|improve this question
What is the response to /login when you call it outside a browser in something like curl, i.e. curl -X POST -d "username=blah&password=blah" <host>/login? –  matt b Sep 28 '12 at 18:44
username=asdf&password=asdf&button=Submit --> the response is fail –  user1460015 Sep 28 '12 at 18:48
Are you including either a urlencoded or bodyParser in the app's configuration? –  Jonathan Lonowski Sep 28 '12 at 18:57
I have the bodyParser but not the urlencoded. This post says bodyParser 'wraps three other methods - express.json, express.urlencoded and express.multipart.' Do you think I would need to explicitly state it? –  user1460015 Sep 28 '12 at 19:04
@user1460015 No. You should only need one or the other. –  Jonathan Lonowski Sep 28 '12 at 19:14
add comment

1 Answer

up vote 4 down vote accepted

You still have to stop the <form> from submitting via its default action, which can be done with event.preventDefault():

$('#login').submit(function(evt) {
  // ...

Otherwise, the <form> will redirect the page to its action (or back to the current address if no action was given), interrupting the $.post request.

share|improve this answer
That did it. Preventing the default action correctly sent the response from the AJAX request to the processData function. –  user1460015 Sep 28 '12 at 19:24
add comment

Your Answer


