I'm trying to get the values I'm sending for an ajax post in my node application. Using this post as a guide, I have this so far:
In Node:
var express = require('express');
var app = express();
var db = require('./db');
app.get('/sender', function(req, res) {
res.sendfile('public/send.html');
});
app.post('/send_save', function(req, res) {
console.log(req.body.id)
console.log(req.body.title);
console.log(req.body.content);
res.contentType('json');
res.send({ some: JSON.stringify({response:'json'}) });
});
app.listen(3000);
On on the AJAX side:
$('#submit').click(function() {
alert('clicked')
console.log($('#guid').val())
console.log($('#page_title').val())
console.log($('#page-content').val())
$.ajax({
url: /send_save,
type: POST,
dataType: json,
data: {
id: $('#guid').val(),
title: $('#page_title').val(),
content: $('#page-content').val()
},
contentType: application/json,
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
console.log(data);
console.log('process sucess');
},
error: function() {
console.log('process error');
},
});
})
This issue is that I can't req.body.id (and any other value like title or content), I'm getting this error in node:
TypeError: Cannot read property 'id' of undefined
If I comment those calls out though, the ajax is successful. I am lost. Am I forgetting something?