225

I want to send an array as an Ajax request:

info[0] = 'hi';
info[1] = 'hello';

$.ajax({
  type: "POST",
  url: "index.php",
  success: function(msg){
    $('.answer').html(msg);
  }
});

How can I do this?

Community
  • 1
  • 1
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
  • solution here: http://stackoverflow.com/questions/713884/convert-js-array-to-json-object-for-use-with-jquery-ajax – Z4k4r14 Sep 21 '12 at 01:55
  • type: 'POST', url: '/url/action/', data: { param1: 1, id: "text" }, dataType: 'text', – Paul Aug 26 '14 at 08:22

3 Answers3

282
info = [];
info[0] = 'hi';
info[1] = 'hello';


$.ajax({
   type: "POST",
   data: {info:info},
   url: "index.php",
   success: function(msg){
     $('.answer').html(msg);
   }
});
Diode
  • 24,570
  • 8
  • 40
  • 51
  • 11
    yes...that worked..thanks..one more thing can be done ...that is initialize **info = {};** then **data: info,** – Poonam Bhatt Jan 18 '12 at 04:45
  • 2
    it sends 0=hi&1=hello, will it work ? depends on your server side code. info = {} is a plain object in Javascript. info = [] is an array object. – Diode Jan 18 '12 at 05:18
  • How would you do it if you want `info=arrayasvalues` instead of every key is a param? – Andres Ramos Sep 27 '16 at 15:35
  • do you mean info=0,1 ? – Diode Sep 27 '16 at 15:57
  • I am doing this, with my C# controller having both a `List` and a `string[]` parameter, but in both cases, the array being past ends up being one comma-delimited string in the first element of the `List` or `string[]` param. Any thoughts ? – Joe Apr 24 '18 at 15:58
77

Just use the JSON.stringify method and pass it through as the "data" parameter for the $.ajax function, like follows:

$.ajax({
    type: "POST",
    url: "index.php",
    dataType: "json",
    data: JSON.stringify({ paramName: info }),
    success: function(msg){
        $('.answer').html(msg);
    }
});

You just need to make sure you include the JSON2.js file in your page...

FarligOpptreden
  • 5,013
  • 22
  • 23
  • I needed to add `JSON.stringify(...)` Thanks for the assist! – ChaseHardin Mar 30 '16 at 17:48
  • In my case, requested key-value map was wrong with above code. I changed "data" line to: data: { paramName: JSON.stringify(info)}, – Shogg Jun 26 '17 at 10:37
  • I tried this, with my C# controller having both a `List` and a `string[]` parameter, but in both cases, the array being past ends up being one comma-delimited string in the first element of the `List` or `string[]` param. Any thoughts? – Joe Apr 24 '18 at 15:59
  • @Joe Try specifying dataType in the $.ajax call as "application/json". If you're using Web API, make sure to decorate the the parameter to your controller with the [FromBody] attribute so it can be deserialized correctly. – FarligOpptreden Apr 24 '18 at 17:48
  • In C#, its need to be deserialized JsonSerializer.Deserialize (params) – PrashSE Oct 11 '21 at 06:49
-3

NOTE: Doesn't work on newer versions of jQuery.

Since you are using jQuery please use it's seralize function to serialize data and then pass it into the data parameter of ajax call:

info[0] = 'hi';
info[1] = 'hello';

var data_to_send = $.serialize(info);

$.ajax({
    type: "POST",
    url: "index.php",
    data: data_to_send,
    success: function(msg){
        $('.answer').html(msg);
    }
});
DivinesLight
  • 2,398
  • 2
  • 24
  • 30