1

I'm making a simple post:

jQ.post(url, {id:1, id:2, id:3});

However, jQuery only posts one of the "id" parameters, value 3 being the latest it sends id=3. How to make it send all of them, so the output is without array brackets?

id=1&id=2&id=3
Yan
  • 582
  • 2
  • 6
  • 22
  • 2
    not possible. you're defining the same object key 3 times, so only the LAST one in the chain gets left. you probably want `{id:[1,2,3]}` instead. – Marc B Nov 24 '15 at 21:47
  • i think the issue here is that the parameter object has the same property being set 3 times, you should try making an array as detailed in this post: http://stackoverflow.com/questions/950673/jquery-post-array – Terry Kernan Nov 24 '15 at 22:30
  • Use JSON.Stringify on Marc B's input and provide other details to post call. Refer to this if you are trying ajax call http://stackoverflow.com/questions/12693947/jquery-ajax-how-to-send-json-instead-of-querystring – pratikpawar Nov 25 '15 at 00:56

2 Answers2

2

Send an array

jQ.post(url, {ids:[1, 2, 3]});
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • it adds [] to the "id": id[]=1&id[]=2&id[]=3, but sends, though I need them without [] – Yan Nov 24 '15 at 21:53
  • @Yan, `id[]=1&id[]=2....` is standard query string syntax used to signify an array. In your backend, you can change your code logic to deal with an array of ids – AmmarCSE Nov 24 '15 at 21:55
  • I understand but unfortunately, it is configured to receive the sequence posted above. – Yan Nov 24 '15 at 21:58
  • @Yan, out of curiosity, what is the backend language/framework you are using? `php`, `.net`, `java`,....? – AmmarCSE Nov 24 '15 at 21:58
  • I'm not sure which backend it uses, but most likely PHP. I can also confirm that the website does send then without [] by looking at post parameters. So there should be a way to do that. – Yan Nov 24 '15 at 22:07
  • Correction it is running .NET – Yan Nov 24 '15 at 22:15
0

To do this:

var obj = {id: [1,2,3]};
var serializedObj = $.param(obj, true);

$.post(url,serializedObj,function(){});

Original answer by Kevin Bowersox JQuery parameter serialization without the bracket mess

Community
  • 1
  • 1
Yan
  • 582
  • 2
  • 6
  • 22