1

Is this even possible? I've been searching and reading for hours and 99% of the posts are about JSON. I just have an object array I want to POST, nothing special.

var arr = [];
arr.push({"name":"steve", "age":35});
arr.push({"name":"sam", "age": 25});

on the other end (node.js), I get this:

{ steve: '', sam: '' }

I'd be incredibly happy to get something similar to

[{"name":"steve", "age":35},
{"name":"sam", "age": 25}]

I feel like I'm missing something incredibly simple, because this seems trivial. I'm not constructing the array myself and this is the format I receive it in. I figured it would be something I could just throw into an $.ajax() post and grab it on the other side.

I've tried so many different combinations of the ajax call

$.ajax({
    method: "POST",
    data: theArray,
    url: someAddress
})

with type instead of method, contentType, dataType, etc, etc with no luck. Spent a whole bunch of time on http://api.jquery.com/jQuery.ajax/ but everyone wants to send JSON. I tried JSON.stringify() on the client, but JSON.parse()on the server side results in giving me data just like my "on the other end..." example above.

4 Answers 4

4

You should use like so data is sent as json string.

$.ajax({
    method: "POST",
    data: JSON.stringify(theArray),
    content type:" application/json",
    url: someAddress
}) 
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I was posting from my mobile, so hard to do formatting
1

Try something

   $.ajax({
        url: "testcookie.php",
        type: "POST",
        data: {
            'arr[]': JSON.stringify(arr)
        }
    });

1 Comment

using $.ajax({ type: 'POST', data: { 'arr': JSON.stringify(arr) }, url: 'http://localhost:3000/people/new' }) gets it through. var d = JSON.parse(req.body.arr); console.log(d[0].name); outputs steve. Perfect.
1

A post request is expected to be a key-value pair.
If you give your array a key:

$.ajax({
  method: "POST",
  data: JSON.stringify({"myArray": theArray}),
  url: someAddress
})

You should be able to retrieve it from the Post body 'myArray' key on the other side.

4 Comments

node.js receives it as { '{"myArray":[{"name":"steve","age":35},{"name":"sam","age":25}]}': '' } req.body.myArray = undefined, req.body[0] = undefined, req.body['myArray'] = undefined, JSON.parse(req.body) gives a SyntaxError. Not sure where to go from here.
Node.js should receive it as a string '{"myArray":[{"name":"steve","age":35},{"name":"sam","age":25}]}' Which you then convert into an object: var theObject = JSON.parse(data); Then you should be able to use it with theObject.myArray.
$.ajax({ type: 'POST', data: JSON.stringify({"myArray": theArray}), url: 'http://localhost:3000/people/new' })
Try without the JSON.stringify (and remove the ; after the url).
0

Convert that object to a String using String() function or use JSON.stringify().

$.ajax({
    method: "POST",
    data: String(theArray),
    url: someAddress
});

1 Comment

Using this gives me an output on the server side as { '[object Object],[object Object]': '' }

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.