2

Suppose I have sent data with the following code:

        $.ajax({
            type: "POST",
            url: "/save/" + #{key},
            data: transitions2,
            success: function (data) {

            },
            dataType: "json"
        });

where transitions2 is hierarchical JS object.

Now how can I receive it intact at server side

router.post('/save/:key', function(req, res) {
    // where is my data here?    
});

UPDATE

I found info about body parsers, and found that my site template already contained them. Particularly, app.js contains:

...
var bodyParser = require('body-parser');
...
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/data', express.static(path.join(__dirname, '../data')));

app.use('/', index);
...

So I wrote in my index.js:

...
router.post('/save/:key', function(req, res) {
    var transitions = req.body;
    image_data.save_transitions(req.params.key, req.query.postfix, transitions);
});
...

Unfortunately, transitions contains

enter image description here

while on client side it contained

enter image description here

i.e. was full of data.

What can be the problem?

UPDATE 2

I tried to do

        $.ajax({
            type: "POST",
            url: "/save/" + #{key},
            data: JSON.stringify(transitions2),
            success: function (data) {

            }
        });

and I see in Fiddler2 now, that full Json is passed.

[{"start_image":"20170402_1_NATURAL_COL0R","end_image":"20170409_1_NATURAL_COL0R","transition_classes":["no_transition","some_activity"]},...

Unfortunately, on server side I observe truncated and corrupted string

enter image description here

(equal sign should not be in JSON).

And JSON.parse fails.

Dims
  • 47,675
  • 117
  • 331
  • 600

3 Answers3

3

use body-parser middleware to retrieve the data.

npm install body-parser

configure this in express app.

Find below the sample code

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

Then in your router use the following:

 router.post('/save/:key', function(req, res) {
        var data = req.body // here is your data   
    });
M.Navy
  • 155
  • 7
1

The problem was on client side only. Correc way to post complex object with json is:

        $.ajax({
            type: "POST",
            url: "/save/" + #{key},
            data: JSON.stringify(transitions2),
            contentType: "application/json; charset=utf-8",
            success: function (data) {

            }
        });

stringify and contentType are obligatory.

Dims
  • 47,675
  • 117
  • 331
  • 600
0

front:

axios.post('/attack', {
        number:number,
        count:count
        }, 
        {
            headers:{contentType: "application/json; charset=utf-8"}
        })
        .then((response) => {
            console.log(response);
        })
        .catch((error) => {
            console.log(error);
        });
}

back:

const express = require('express')
const bodyParser = require('body-parser')
const app = express()

app.use(bodyParser.json())

app.post('/attack', (req, res) => {
    let data = req.body
    console.log(data)
    res.send('200')
})

console log: { number: '(number)', count: '(count)' }

Angela Amarapala
  • 1,002
  • 10
  • 26
Dmitry
  • 1