POST request empty (body-parser extended: true) - still not working?
I am stumped. I am making a simple fetch() to the backend where I log request body i.e. console.log(req.body.form_data)
Problem: returning {} empty json object.
Attempt: I have installed body-parser i.e
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true })); // to support URL-encoded bodies
What is wrong?
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true // to support URL-encoded bodies
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
let users = ;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/', function(req, res, next) {
if(req.body.form_data) {
console.log(req.body.form_data)
// res.json(users);
}
console.log(req.body)
res.render('index', { title: 'TO DO APP' });
});
module.exports = router;
myscript.js
document.getElementById("listForm").addEventListener('submit', function(e) {
e.preventDefault();
const fname = document.getElementById("fname").value;
const list_name = document.getElementById("list-name").value;
const list_date = document.getElementById("list-date").value;
var form_data = {
fname: fname,
list_name: list_name,
list_date: list_date
};
console.log(form_data );
// AJAX fetch()
fetch('/', {
method: 'POST',
headers: new Headers(),
body: form_data
}).then(function(response) {
console.log("response..");
return response.json();
}).then(function(user_json) {
console.log(user_json);
});
});
index.pug
extends layout
block content
h1= title
p First, create your list
form#listForm(method="POST" action="/")
input(type='text' id="fname" name='fname' autofocus placeholder="your name")
input(type='text' id="list-name" name='list-name' autofocus placeholder="name your list")
input(type='date' id="list-date" name='list-date' autofocus placeholder="today's date")
button(type='submit') Create List
javascript node.js json api body-parser
add a comment |
I am stumped. I am making a simple fetch() to the backend where I log request body i.e. console.log(req.body.form_data)
Problem: returning {} empty json object.
Attempt: I have installed body-parser i.e
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true })); // to support URL-encoded bodies
What is wrong?
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true // to support URL-encoded bodies
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
let users = ;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/', function(req, res, next) {
if(req.body.form_data) {
console.log(req.body.form_data)
// res.json(users);
}
console.log(req.body)
res.render('index', { title: 'TO DO APP' });
});
module.exports = router;
myscript.js
document.getElementById("listForm").addEventListener('submit', function(e) {
e.preventDefault();
const fname = document.getElementById("fname").value;
const list_name = document.getElementById("list-name").value;
const list_date = document.getElementById("list-date").value;
var form_data = {
fname: fname,
list_name: list_name,
list_date: list_date
};
console.log(form_data );
// AJAX fetch()
fetch('/', {
method: 'POST',
headers: new Headers(),
body: form_data
}).then(function(response) {
console.log("response..");
return response.json();
}).then(function(user_json) {
console.log(user_json);
});
});
index.pug
extends layout
block content
h1= title
p First, create your list
form#listForm(method="POST" action="/")
input(type='text' id="fname" name='fname' autofocus placeholder="your name")
input(type='text' id="list-name" name='list-name' autofocus placeholder="name your list")
input(type='date' id="list-date" name='list-date' autofocus placeholder="today's date")
button(type='submit') Create List
javascript node.js json api body-parser
Try using this:var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in theindex.js
. But check your console to see if you are really making any AJAX calls.
– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
are you passing headerscontent-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.
– vibhor1997a
Nov 24 '18 at 9:51
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26
add a comment |
I am stumped. I am making a simple fetch() to the backend where I log request body i.e. console.log(req.body.form_data)
Problem: returning {} empty json object.
Attempt: I have installed body-parser i.e
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true })); // to support URL-encoded bodies
What is wrong?
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true // to support URL-encoded bodies
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
let users = ;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/', function(req, res, next) {
if(req.body.form_data) {
console.log(req.body.form_data)
// res.json(users);
}
console.log(req.body)
res.render('index', { title: 'TO DO APP' });
});
module.exports = router;
myscript.js
document.getElementById("listForm").addEventListener('submit', function(e) {
e.preventDefault();
const fname = document.getElementById("fname").value;
const list_name = document.getElementById("list-name").value;
const list_date = document.getElementById("list-date").value;
var form_data = {
fname: fname,
list_name: list_name,
list_date: list_date
};
console.log(form_data );
// AJAX fetch()
fetch('/', {
method: 'POST',
headers: new Headers(),
body: form_data
}).then(function(response) {
console.log("response..");
return response.json();
}).then(function(user_json) {
console.log(user_json);
});
});
index.pug
extends layout
block content
h1= title
p First, create your list
form#listForm(method="POST" action="/")
input(type='text' id="fname" name='fname' autofocus placeholder="your name")
input(type='text' id="list-name" name='list-name' autofocus placeholder="name your list")
input(type='date' id="list-date" name='list-date' autofocus placeholder="today's date")
button(type='submit') Create List
javascript node.js json api body-parser
I am stumped. I am making a simple fetch() to the backend where I log request body i.e. console.log(req.body.form_data)
Problem: returning {} empty json object.
Attempt: I have installed body-parser i.e
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true })); // to support URL-encoded bodies
What is wrong?
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// body parser - parse json in body.requests
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({
extended: true // to support URL-encoded bodies
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
let users = ;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/', function(req, res, next) {
if(req.body.form_data) {
console.log(req.body.form_data)
// res.json(users);
}
console.log(req.body)
res.render('index', { title: 'TO DO APP' });
});
module.exports = router;
myscript.js
document.getElementById("listForm").addEventListener('submit', function(e) {
e.preventDefault();
const fname = document.getElementById("fname").value;
const list_name = document.getElementById("list-name").value;
const list_date = document.getElementById("list-date").value;
var form_data = {
fname: fname,
list_name: list_name,
list_date: list_date
};
console.log(form_data );
// AJAX fetch()
fetch('/', {
method: 'POST',
headers: new Headers(),
body: form_data
}).then(function(response) {
console.log("response..");
return response.json();
}).then(function(user_json) {
console.log(user_json);
});
});
index.pug
extends layout
block content
h1= title
p First, create your list
form#listForm(method="POST" action="/")
input(type='text' id="fname" name='fname' autofocus placeholder="your name")
input(type='text' id="list-name" name='list-name' autofocus placeholder="name your list")
input(type='date' id="list-date" name='list-date' autofocus placeholder="today's date")
button(type='submit') Create List
javascript node.js json api body-parser
javascript node.js json api body-parser
asked Nov 24 '18 at 9:22
ShazShaz
528617
528617
Try using this:var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in theindex.js
. But check your console to see if you are really making any AJAX calls.
– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
are you passing headerscontent-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.
– vibhor1997a
Nov 24 '18 at 9:51
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26
add a comment |
Try using this:var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in theindex.js
. But check your console to see if you are really making any AJAX calls.
– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
are you passing headerscontent-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.
– vibhor1997a
Nov 24 '18 at 9:51
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26
Try using this:
var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in the index.js
. But check your console to see if you are really making any AJAX calls.– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
Try using this:
var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in the index.js
. But check your console to see if you are really making any AJAX calls.– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
are you passing headers
content-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.– vibhor1997a
Nov 24 '18 at 9:51
are you passing headers
content-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.– vibhor1997a
Nov 24 '18 at 9:51
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26
add a comment |
1 Answer
1
active
oldest
votes
I suppose you need to pass the content-type
header with your request which you're not sending currently. Also you would need to convert it into a JSON string by using JSON.stringify
like below.
let headers = new Headers();
headers.append('content-type','application/json');
fetch('/', {
method: 'POST',
headers: headers,
body: JSON.stringify(form_data)
}).then(function (response) {
console.log("response..");
return response.json();
}).then(function (user_json) {
console.log(user_json);
});
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should showreq.body.form_data
asundefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys likereq.body.firstName
and something like that. I don't think what you are trying to do is a good idea.
– vibhor1997a
Nov 25 '18 at 2:24
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456807%2fpost-request-empty-body-parser-extended-true-still-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I suppose you need to pass the content-type
header with your request which you're not sending currently. Also you would need to convert it into a JSON string by using JSON.stringify
like below.
let headers = new Headers();
headers.append('content-type','application/json');
fetch('/', {
method: 'POST',
headers: headers,
body: JSON.stringify(form_data)
}).then(function (response) {
console.log("response..");
return response.json();
}).then(function (user_json) {
console.log(user_json);
});
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should showreq.body.form_data
asundefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys likereq.body.firstName
and something like that. I don't think what you are trying to do is a good idea.
– vibhor1997a
Nov 25 '18 at 2:24
|
show 3 more comments
I suppose you need to pass the content-type
header with your request which you're not sending currently. Also you would need to convert it into a JSON string by using JSON.stringify
like below.
let headers = new Headers();
headers.append('content-type','application/json');
fetch('/', {
method: 'POST',
headers: headers,
body: JSON.stringify(form_data)
}).then(function (response) {
console.log("response..");
return response.json();
}).then(function (user_json) {
console.log(user_json);
});
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should showreq.body.form_data
asundefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys likereq.body.firstName
and something like that. I don't think what you are trying to do is a good idea.
– vibhor1997a
Nov 25 '18 at 2:24
|
show 3 more comments
I suppose you need to pass the content-type
header with your request which you're not sending currently. Also you would need to convert it into a JSON string by using JSON.stringify
like below.
let headers = new Headers();
headers.append('content-type','application/json');
fetch('/', {
method: 'POST',
headers: headers,
body: JSON.stringify(form_data)
}).then(function (response) {
console.log("response..");
return response.json();
}).then(function (user_json) {
console.log(user_json);
});
I suppose you need to pass the content-type
header with your request which you're not sending currently. Also you would need to convert it into a JSON string by using JSON.stringify
like below.
let headers = new Headers();
headers.append('content-type','application/json');
fetch('/', {
method: 'POST',
headers: headers,
body: JSON.stringify(form_data)
}).then(function (response) {
console.log("response..");
return response.json();
}).then(function (user_json) {
console.log(user_json);
});
answered Nov 24 '18 at 11:15
vibhor1997avibhor1997a
1,5701624
1,5701624
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should showreq.body.form_data
asundefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys likereq.body.firstName
and something like that. I don't think what you are trying to do is a good idea.
– vibhor1997a
Nov 25 '18 at 2:24
|
show 3 more comments
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should showreq.body.form_data
asundefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys likereq.body.firstName
and something like that. I don't think what you are trying to do is a good idea.
– vibhor1997a
Nov 25 '18 at 2:24
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Okay turns out console.log(req.body) is printing the request BUT NOT console.log(req.body.form_data)... any ideas??
– Shaz
Nov 25 '18 at 2:10
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should show
req.body.form_data
as undefined
– vibhor1997a
Nov 25 '18 at 2:12
Because you haven't passed form_data as field, fields are fname, list_name, list_date. It should show
req.body.form_data
as undefined
– vibhor1997a
Nov 25 '18 at 2:12
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
Oh. In my backend i need to check for different post requests i.e. if(form_data) if(form2_data) etc.. what is the right way of sending my json object to make it work like this?
– Shaz
Nov 25 '18 at 2:18
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
How would you do it? send an array with json object as elements is the only method i can think of
– Shaz
Nov 25 '18 at 2:21
Why do you need to that way? Isn't it the correct way? You should access the keys like
req.body.firstName
and something like that. I don't think what you are trying to do is a good idea.– vibhor1997a
Nov 25 '18 at 2:24
Why do you need to that way? Isn't it the correct way? You should access the keys like
req.body.firstName
and something like that. I don't think what you are trying to do is a good idea.– vibhor1997a
Nov 25 '18 at 2:24
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456807%2fpost-request-empty-body-parser-extended-true-still-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try using this:
var router = express();
I don't know if that makes a difference, but try. Strongly feel the problem is in theindex.js
. But check your console to see if you are really making any AJAX calls.– Praveen Kumar Purushothaman
Nov 24 '18 at 9:23
u mean: var router = express.Router(); to var router = express(); .. didn't change anything
– Shaz
Nov 24 '18 at 9:27
are you passing headers
content-type: application/json
?? Without it the body parser won't be able to know that you've passed json and it needs to parse it.– vibhor1997a
Nov 24 '18 at 9:51
No i am using headers() as written in fetch() body... would do you mean? example
– Shaz
Nov 24 '18 at 10:12
inside index.js what is result of this line: console.log(req.body) can you add your log results?
– behzad.robot
Nov 24 '18 at 11:26