最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Receiving Jquery POST data in Express - Stack Overflow

programmeradmin3浏览0评论

Edit See the accepted answer below for the fix. I also had to remove the line contentType: 'appliction/json', from my POST request.

I'm trying to send a string to Node.js / Express, but req.body is undefined server-side.

Client jQuery:

$.post({
        traditional: true,
        url: '/matches',
        contentType: 'appliction/json',
        data: viewedProfiles,
        dataType: 'json',
        success: function(response){

Express:

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.post('/matches', isLoggedIn, function(req, res){
  console.log(req.body) // this is undefined
  var loadedProfiles = []
  loadedProfiles.push(req.body.viewedProfiles)
  console.log('loadedProfiles')
  console.log(loadedProfiles)

I've tried:

  • not specifying 'dataType'
  • setting data: JSON.stringify(viewProfiles)
  • splitting the string into an array on the client, and then having jQuery stringify it
  • looking for req.params instead of req.body (clutching at straws)

I can see the XHR request in dev tools, and it contains the string I'm expecting.

What super obvious thing am I missing to send the data to Express?

Thanks.

Edit See the accepted answer below for the fix. I also had to remove the line contentType: 'appliction/json', from my POST request.

I'm trying to send a string to Node.js / Express, but req.body is undefined server-side.

Client jQuery:

$.post({
        traditional: true,
        url: '/matches',
        contentType: 'appliction/json',
        data: viewedProfiles,
        dataType: 'json',
        success: function(response){

Express:

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.post('/matches', isLoggedIn, function(req, res){
  console.log(req.body) // this is undefined
  var loadedProfiles = []
  loadedProfiles.push(req.body.viewedProfiles)
  console.log('loadedProfiles')
  console.log(loadedProfiles)

I've tried:

  • not specifying 'dataType'
  • setting data: JSON.stringify(viewProfiles)
  • splitting the string into an array on the client, and then having jQuery stringify it
  • looking for req.params instead of req.body (clutching at straws)

I can see the XHR request in dev tools, and it contains the string I'm expecting.

What super obvious thing am I missing to send the data to Express?

Thanks.

Share Improve this question edited Oct 12, 2016 at 15:25 Runny Yolk asked Oct 12, 2016 at 14:43 Runny YolkRunny Yolk 1,1644 gold badges25 silver badges41 bronze badges 2
  • Do you have connect middleware running in your page? – David R Commented Oct 12, 2016 at 14:48
  • I don't but I have jQuery data posts running successfully elsewhere in the app. – Runny Yolk Commented Oct 12, 2016 at 14:55
Add a ment  | 

2 Answers 2

Reset to default 7

Your server side code looks fine but you need to use $.ajax() rather than $.post() function because $.post() function send data into url (with url encoded). So your JQuery code would be

$.ajax({
        url: '/matches',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({"viewedProfiles": viewedProfiles}),
        success: function(response){

I hope this will help you

I've configured an exact same setup. And code below works:

var profiles = { 'data' : 'hello' };

$.post({
        traditional: true,
        url: '/matches',
        contentType: 'application/json',
        data: JSON.stringify( profiles ),
        dataType: 'json',
        success: function(response){ console.log( response ); }
} );

My nodejs engine:

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.post( '/matches' , function(req, res){
  console.log(req.body) // this outputs: { data: 'hello' }
} );

Btw, There is a typo where your contentType is, 'applicAtion/json'

发布评论

评论列表(0)

  1. 暂无评论