develop1
develop1

Reputation: 767

Nodejs getting [object Object] from request

I am passing variable from AJAX to Node JS server but, when trying to extract the data from the request I keep getting [Object Object]. How would I get the data passed from AJAX to Node ?

AJAX:

$.ajax({
            type: "POST", 
            url: "/Watch", 
            data: {"name" : stockName},
            contentType: "json", 
            success: function(d) {
                //Stuff happening
            }, 
            error: function(d) {
                console.log("Error");
            }
        });

Node JS Server:

app.post('/Watch', function(req, res) {     

console.log("DATA from AJAX = " + req.body);//Returns [Object Object]
console.log("DATA from AJAX = " + req.body.data);//Returns 'undefined'
console.log("DATA from AJAX = " + req.data);//Returns 'undefined'
console.log("DATA from AJAX = " + req.name);//Returns 'undefined'
console.log("DATA from AJAX = " + req.body.data);//Returns 'undefined'  

res.send("");
});

In the Node JS the console.log shows some of the different thing I tried to get the data from the request.

Upvotes: 1

Views: 6491

Answers (2)

Arun Panneerselvam
Arun Panneerselvam

Reputation: 2335

Use res.format to format and send to the content-type requested,

res.format({
  'text/plain': function(){ 
   res.send( 'some json string inside quotes' );
},
 'text/html': function(){
  res.send('sending html response');
               },
'application/json': function(){
  res.json({ 
     success: true,
     messge: 'what you requested',
  });
},
'default': function() {
  res.status(406).send('Invalid format requested!');
}
});

you can populate the data anyway you need. on javascript, just evaluate the string on get/post eval('('+ msg + ')');

$.ajax { 
...
success: function (msg, statusText) {
     console.log("Response: " + msg);
     msg = eval('('+ msg + ')');
     $('.result').html(msg.status);
     console.log(msg);

},
....
});

Upvotes: 1

develop1
develop1

Reputation: 767

With the help of @mscdex the solution was

AJAX :

alert(JSON.stringify({"name" : stockName}));
        $.ajax({
            type: "POST", 
            url: "/Watch", 
            data: JSON.stringify({"name" : stockName}), 
            contentType: "application/json", 
            success: function(d) {
                //Stuff                 
            }, 
            error: function(d) {
                console.log("Error");
            }
        }); 

Node JS:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var port = process.env.PORT || 5000;

app.use(bodyParser.json());   

app.post('/Watch', function(req, res) {
    console.log("In Watchlist POST");

    console.log("DATA from AJAX = " + req.body.name);//This was the solution    

    res.send("Finished Watchlist post");
});

app.use(express.static('public'));

app.listen(port, function () {
    console.log('App listening on port ' + port);
});

Upvotes: 2

Related Questions