javascript - Express.js access fields of serialized Form Data -
i struggling accessing specific fields of serialized formdata in express router.
this ajax request:
var formdata = $("#add-fut-account-form").find("select, textarea, input").serialize(); $.ajax({ url: "/accounts/insert", type: "post", data: {formdata}, success: function (response) { $("#addfutaccountresponse").append(response); }, error: function (xhr) { alert("error while trying insert new account!"); } })
this router:
router.post('/insert', function(req, res) { var formdata = req.body.formdata; console.log(formdata); });
my console.log() output:
email=kentor%40gmail.com&password=dsadsa&secret=123&platform=p&account-type=x&proxy=&rpm=8&threshold=3.5&optional-comment=&is-active=on
my problem:
how should serialize / send / receive formdata can access specific fields (such email, password) form. tried using req.body.formdata.email
doesn't work (result: undefined). know 2 possible ways how solve issue, appear roundabout way. want know best practice handling formdata express is.
edit: using bodyparse in app.js:
app.use(bodyparser.json()); app.use(bodyparser.urlencoded({extended: true}));
try making ajax follows:
var formdata = $("#add-fut-account-form").find("select, textarea, input").serialize(); $.ajax({ url: "/accounts/insert", type: "post", data: formdata, success: function (response) { $("#addfutaccountresponse").append(response); }, error: function (xhr) { alert("error while trying insert new account!"); } })
remove curly braces on data
Comments
Post a Comment