9

I have a server as following:

app.post('/', function(req, res, next) {
   console.log(req);
   res.json({ message: 'pppppppppppppssssssssssssss ' });   
});

The request is sent from a client as:

$.ajax({
    type: "POST",
    url: self.serverURI,
    data: JSON.stringify({ "a": "128", "b": "7" }),
    dataType: 'json',
    success: function (result) {
        console.log(result);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr);
    }
});

so far the connection fine.

My problem is in the server:

console.log(req);

where I want to read the data I sent. How can I read { "a": "128", "b": "7" } from req?

qqilihq
  • 10,794
  • 7
  • 48
  • 89
arslan
  • 2,034
  • 7
  • 34
  • 61

2 Answers2

13

Although you're not mentioning it, your code looks like it's written for an Express environment. My answer is targeted to this.

Make sure to use body-parser for Express. In case, your project depends on some generated boilerplate code, it's most likely already included in your main server script. If not:

var bodyParser = require('body-parser');
app.use(bodyParser.json());

Installation with npm: npm install body-parser --save

The parsed JSON can then be accessed through req.body:

app.post('/', function(req, res, next) {
    console.log(req.body); // not a string, but your parsed JSON data
    console.log(req.body.a); // etc.
    // ...
});
qqilihq
  • 10,794
  • 7
  • 48
  • 89
  • Thank you so much, it worked. Finally ufffffff. It would be great if you could explain how can I send such a JSon to client by "res" parameter :) – arslan Dec 18 '16 at 18:20
  • `res.json(object)` is the correct way to go. As already given in your sample code. – qqilihq Dec 18 '16 at 18:22
  • I read many tutorials. Some use "res.json(object)", some use "res.end(something)", quite confusing for people who new to this. – arslan Dec 18 '16 at 18:23
  • `res.json` will automatically set the response `content-type` header to `application/json`, while `res.send` will set `text/html` by default. If you're returning JSON, definitely use the `res.json` function. – qqilihq Dec 18 '16 at 18:26
  • More details here: http://stackoverflow.com/questions/19041837/difference-between-res-send-and-res-json-in-express-js – qqilihq Dec 18 '16 at 18:27
  • I am trying to send a large data as JSon, is there any example of constructing JSon data and send back? I think I need to define a function for construct JSon and send by "res.json(data)". Could you show me an example of it? I would appreciate it – arslan Dec 18 '16 at 18:29
  • To keep things here on topic, rather open a new post for this. I can have a look later on :) – qqilihq Dec 18 '16 at 18:42
  • I try myself first, then ask if have troubles :) thx, you helped a lot – arslan Dec 18 '16 at 18:47
  • Well is that really something that can't be solved without external library? – Tomáš Zato Sep 25 '17 at 22:33
5

For Express 4+,

const express = require("express");
const app = express();

app.use(express.json());

Then, you can use req.body as expected.

app.post("/api", (req, res) => {
  /*
    If the post request included { data: "foo" },
    then you would access `data` like so:
  */
  req.body.data
  ...
});
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158