0

I'm trying to send an object that looks like this

var postBody = {
        id: userID,
        objectID: [0, 1],
        objects: [
            {id: 0, x: 0.33930041152263374, y: 0.08145246913580247, width: 0.0823045267489712, height: 0.30864197530864196},
            {id: 1, x: 0.5277777777777778, y: 0.08453888888888889, width: 0.0823045267489712, height: 0.30864197530864196}
        ]
    };

this is ajax call

$.ajax({
        type: 'POST',
        url: url,
        data: postBody,
        dataType: 'JSONP',
        success: function(data) {
            console.log(data);
        },
        error: function(err) {
            console.log(err);
        }
    });

this is what it looks like on server

 { id: '583f137fc338b517ec467643',
'objectID[]': [ '0', '1' ],
'objects[0][id]': '0',
'objects[0][x]': '0.33930041152263374',
'objects[0][y]': '0.08145246913580247',
'objects[0][width]': '0.0823045267489712',
'objects[0][height]': '0.30864197530864196',
'objects[1][id]': '1',
'objects[1][x]': '0.5277777777777778',
'objects[1][y]': '0.08453888888888889',
'objects[1][width]': '0.0823045267489712',
'objects[1][height]': '0.30864197530864196' }

if I put data: JSON.stringify(postBody) this is what I get

{ '{"id":"583f137fc338b517ec467643","objectID":[0,1],"objects":[{"id":0,"x":0.5,"y":0.5,"width":0.1,"height":0.1},{"id":1,"x":0.5401234567901234,"y":0.1833043209876543,"width":0.0823045267489712,"height":0.30864197530864196}]}': '' }

And it works! but then I cannot JSON.parse() it this is what I get when I try

TypeError: Cannot convert object to primitive value

and all that Im doing on data on backend is this

console.log(JSON.parse(req.body)); // in the first example it was the same thing but only without JSON.parse()

Anyone have an idea why this is happening here? Even if you have a suggestion on what I could try feel free to post here, otherwise I'm gonna have to write a function for parsing these badass inputs lol.

2
  • I think the response you're trying to JSON.parse is already a json object being returned by the server. Can you post the response from the sever? Commented Nov 30, 2016 at 18:51
  • @Lex - I think you're right, depending on op's server setup - the response is already an object Commented Nov 30, 2016 at 18:56

1 Answer 1

1

All the parsing/stringifying is handled by Node, so don't worry about it, just pass your object as it is, and you'll be able to access its properties in req.body.

client :

$.ajax({
            type: 'POST',
            url: url,
            data: postBody,
            dataType: 'json',
            success: function(data) {
                console.log(data);
            },
            error: function(err) {
                console.log(err);
            }
        });

server :

 console.log(req.body);  //should give you [Object object]

you can then send it right back with: res.status(200).json(req.body));

and read it in your ajax callback :

success: function(data) {
    console.log(data);
},
Sign up to request clarification or add additional context in comments.

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.