Human Cyborg Relations Human Cyborg Relations - 1 year ago 70
Ajax Question

Ajax POST request sending undefined to Express

I'm trying to send some data to be parsed. The script on the client side is the following:

function addURL(link) {
console.log("Adding url...");
type: "POST",
url: location.protocol + "//" + + "/shorturl/create",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify({url:link}),
success: function(data){
error: function(err){
console.log("Ran into an error... " + err);

On my express app, on one of the routers, I've got:"/create", function(req, res){

var url = req.body.url; }

I get 'undefined', and then the 'Cannot get property 'url' of undefined'.

I can't figure out where I'm going wrong...

Answer Source

You need to use bodyParser:


var bodyParser = require('body-parser')
  , app = express();

app.use(bodyParser.urlencoded({extended: true}));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download