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.

5
  • Possible duplicate of How to retrieve POST query parameters? Commented Jun 16, 2017 at 12:18
  • @dan didn't work with body parsers though Commented Jun 16, 2017 at 12:32
  • Have you tried stringifying your JSON before you send it? i.e. replace data: transitions2 with data: JSON.stringify(transitions2). Commented Jun 16, 2017 at 12:54
  • @dan yes just tried an failed Commented Jun 16, 2017 at 13:12
  • @dan see my update pls Commented Jun 16, 2017 at 13:13

3 Answers 3

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   
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I found this, but it didn't help.
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.

Comments

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)' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.