0

I'm sending parameters from a browser to a server with jQuery ajax. But I cannot get my params on a server side. Here is my code:

// Front-end
    $.post( "/foo", {one: "two"}, function( data ) {
        $( "#cout" ).html( data );
    });


// backend
app.all('/foo', function(req, res) {
    console.log("[req.body] :", req.body);   // [req.body] : undefined
    console.log("[req.params] :", req.params);   // [req.params] : []
});

How may I get my params?

UPD:

My app.js script:

var fs = require("fs");
var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

app.all('/foo', function(req, res) {
    console.log("[req.body] :", req.body);
    console.log("[req.params] :", req.params);
});

app.listen(3000);
console.log('Listening on port 3000');
2
  • Seems about right, how are you creating the server, and what middleware is added ? Commented Jan 16, 2014 at 16:56
  • 1
    Just for fun, try adding app.use(express.bodyParser()); before the static route Commented Jan 16, 2014 at 17:12

1 Answer 1

1

Add

app.use(express.bodyParser());

Before the static middleware.

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

Comments

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.