node.js - Bad Request - node js ajax post -


i'm using express 3.0 , node v0.11.0. have button submits form , i'm using ajax post data json object node server.
client code is:

$('#contact').submit(function(e) { console.log('submit called'); var formdata = json.stringify($('form').serializeobject()); console.log(formdata);  $.ajax({     url: "http://localhost:3000/savedata/",     type: "post",     datatype: 'json',     data: {objectdata: formdata},     contenttype: "application/json",     complete: function() {       console.log('process complete');     },     success: function(data) {       console.log('process sucess');     },     error: function() {       console.log('process error');     },   }); return false; }); 

then on server:

var express = require('express'); var app = express(); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyparser()); app.use(express.cookieparser()); app.use(express.static(__dirname + '/public', {maxage: 86400000})); app.engine('.jade', require('jade').__express);  app.post('/savedata', function(req, res) {   console.log('savedata called');   console.log(req.body);   //var resultobject = json.parse(req.body.objectdata);   //console.log(resultobject);   res.end(); }); 

the problem i'm facing when use express.bodyparser() middleware, a:

error: bad request @ next (/home/captain/data/lemontreecakes/node_modules/express/node_modules/connect/lib/proto.js:125:13) @ /home/captain/data/lemontreecakes/node_modules/express/node_modules/connect/lib/middleware/bodyparser.js:54:23 @ incomingmessage.<anonymous> (/home/captain/data/lemontreecakes/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60) @ incomingmessage.eventemitter.emit (events.js:92:17) @ _stream_readable.js:883:14 @ process._tickcallback (node.js:415:13) 

this console output in browser:

submit called load.js:33 {"name":"tes","email":"tets@est.com"} load.js:36 post http://localhost:3000/savedata/ 400 (bad request) jquery-1.9.1.min.js:5 process error load.js:54 process complete 

so know i'm submitting data server. if remove body parser middle ware node server,

savedata called typeerror: cannot read property 'objectdata' of undefined 

i'm thinking i'm not sending json data server. hints appreciated.

i'm not sure if solves problem try this:

$.ajax({     ...     data: json.stringify({ "objectdata": formdata}),     ... 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -