yossi
yossi

Reputation: 3164

Submit array of objects using jquery

Is there any built-in solution for posting array of objects, through jquery?

The array is

data = [{
  id: "333",
  date: "22/12/2015"
 },
{
  id: "333",
  date: "22/12/2015"
 }]

$.post('url', data, function(){}, "json"); failed

Upvotes: 0

Views: 60

Answers (2)

gen_Eric
gen_Eric

Reputation: 227240

You need to pass values in a POST as key/value pairs. You can't just send the array, you need to give it a "key" in the POST array.

$.post('url', {data: data}, function(){}, "json");

Upvotes: 0

Diego
Diego

Reputation: 16714

You can send an object that contains the array like this:

data = {
    items: [{
      id: "333",
      date: "22/12/2015"
    },
    {
      id: "333",
      date: "22/12/2015"
    }]
}

$.post('url', data, function(){}, "json");

Upvotes: 1

Related Questions