1

I have built few rest API on server and calling them from other domain the Get request is working fine but I am facing an issue in calling the POST request.

I am unable to receive data on server send by the clients.

Server Code:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
//var fn = require('fn')

var app = express();
var allowCrossDomain = function(req, res, next) {

    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    // intercept OPTIONS method
    if ('OPTIONS' === req.method) {
        res.send(200);
    }
    else {
        next();
    }
};

// all environments
app.set('port', process.env.PORT || 3000);
app.use(allowCrossDomain);


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}
app.post('/user', user.saveUser);


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

exports.saveUser = function(req, res) {
    var key = req.body.key; //fb,twitter,web
    var userData = req.body.userData;
    var result  = checkUser(userData,key);        
}

Clients code where the request is made :

var data = { key: 'web', userData: userData }
$.ajax({
    method: "POST",
    //contentType: 'application/json',
    url: "www.acbd.com/user",
    //url:"http://prayable-21641.onmodulus.net/user",
    data: data,
    crossDomain: true,
    dataType: "json"
}).success(function (data, textstatus) {
    // this callback will be called asynchronously
    // when the response is available

    console.log(data)
    console.log(textstatus)
}).error(function (data, textstatus) {
    console.log(data)
    console.log(textstatus)
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

I am unable to get key or userData on server, it say they are not defined:

TypeError: Cannot read property 'key' of undefined

halfer
  • 18,701
  • 13
  • 79
  • 158

2 Answers2

2

You forgot to require and use the body-parser middleware module.

And also, why is content-type commented out? You need it

Server code should look like this:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
//var fn = require('fn')

//requiring body-parser
var bodyParser = require('body-parser');

var app = express();
var allowCrossDomain = function(req, res, next) {

  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

  // intercept OPTIONS method
  if ('OPTIONS' === req.method) {
      res.send(200);
  }
  else {
      next();
  }
};

// all environments
app.set('port', process.env.PORT || 3000);
app.use(allowCrossDomain);


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

// using body-parser
app.use(bodyParser());

app.post('/user', user.saveUser);


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

also, don't forget to npm install it:

npm install body-parser
oronbz
  • 2,119
  • 2
  • 10
  • 17
0

//REquire the body parser, make sure you install it using

npm install body-parser

var bodyParser = require('body-parser');

and then use it

app.use(bodyParser.json());

app.use(bodyParser.urlencoded());

Hope that helps

Community
  • 1
  • 1
Kiba
  • 8,180
  • 6
  • 24
  • 30