-1

I am trying to pass data from react js to nodejs. Can anyone help, Reactjs code -

$.ajax({
  url: 'http://localhost:8081/getdata',      
  type: 'POST',
  data: {ajaxid:4},
  success: function(data) {
    this.setState({EmpCollection: data.EmpCollection}); 
  }.bind(this),
  error: function(xhr, status, err) {
    console.error(this.props.Empnumber, status, err.toString());
  }.bind(this)

});

Nodejs code-

app.get('/getdata', function (req, res) {
var Client = require('node-rest-client').Client;
var client = new Client();
 var messageData =  "";
 console.log("req="+req);
  client.get("some URL", function (data, response) {            
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");      
  res.send(data);
});
})

How can i get ajaxid:4 in nodejs code?

user2768132
  • 460
  • 4
  • 11
  • 26

2 Answers2

1

I have found one mistake in your code.

you are calling ajax call with POST method but in server side route is enable for GET method.

app.get('/getdata', function (req, res) {

replace with

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

and try

If it is GET request then you can get by using req object. like req.params or req.param('ajaxid')

If it is POST request then you have you use middleware like body-parser and then you can access req.body.ajaxid

Irfan Ali
  • 2,238
  • 1
  • 23
  • 22
  • Yeah tried above but problem is how can i extract ajaxid=4 in node js? – user2768132 Mar 14 '17 at 09:30
  • If it is GET request then you can get by using req object. like req.params or req.param('ajaxid') – Irfan Ali Mar 14 '17 at 09:35
  • If it is POST request then you have you use middleware like body-parser and then you can access req.body.ajaxid. – Irfan Ali Mar 14 '17 at 09:37
  • added body parser through npm but getting req.body= undefined – user2768132 Mar 14 '17 at 09:51
  • There you need to configure body-parser with server config. Are you using express js? – Irfan Ali Mar 14 '17 at 09:55
  • yes... with express i am trying to achieve. also i have added the below lines but still not able to achieve.. var bodyParser = require('body-parser') // create application/json parser var jsonParser = bodyParser.json() // create application/x-www-form-urlencoded parser var urlencodedParser = bodyParser.urlencoded({ extended: false }) //Store Api call app.post('/getdata', function (req, res) { var Client = require('node-rest-client').Client; var client = new Client(); var messageData = ""; if (!req.body) return res.sendStatus(400) console.log("Welocme="+req.body.ajaxid); – user2768132 Mar 14 '17 at 10:03
  • Please try with below format var express = require('express'); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser()); app.use(function (req, res, next) { console.log(req.body) // populated! next(); }) – Irfan Ali Mar 14 '17 at 10:16
1

You need to use the express plugin body-parser to access the POST data.

You can find more info how to do it here: How to retrieve POST query parameters?

Community
  • 1
  • 1
Tobias Lins
  • 2,543
  • 19
  • 22