226

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?

2
  • solution here: stackoverflow.com/questions/713884/… Commented Sep 21, 2012 at 1:55
  • type: 'POST', url: '/url/action/', data: { param1: 1, id: "text" }, dataType: 'text', Commented Aug 26, 2014 at 8:22

3 Answers 3

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


$.ajax({
   type: "POST",
   data: {info:info},
   url: "index.php",
   success: function(msg){
     $('.answer').html(msg);
   }
});
Sign up to request clarification or add additional context in comments.

5 Comments

yes...that worked..thanks..one more thing can be done ...that is initialize info = {}; then data: info,
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.
How would you do it if you want info=arrayasvalues instead of every key is a param?
do you mean info=0,1 ?
I am doing this, with my C# controller having both a List<string> 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<string> or string[] param. Any thoughts ?
78

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...

5 Comments

I needed to add JSON.stringify(...) Thanks for the assist!
In my case, requested key-value map was wrong with above code. I changed "data" line to: data: { paramName: JSON.stringify(info)},
I tried this, with my C# controller having both a List<string> 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<string> or string[] param. Any thoughts?
@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.
In C#, its need to be deserialized JsonSerializer.Deserialize<string[]> (params)
-2

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);
    }
});

11 Comments

This is not the whole true, at least not today. jQuery wouldn't actually auto-serialize just any array. When I tried with simple array of integers, it kept just the last item. Please, check this post for more explanation: stackoverflow.com/a/4239496/261332
Thank you @userfuser I have updated the answer and removed the auto serialize part.
how does this have so many upvotes? There is no such jQuery function as jQuery.serialize(). There is .serialize() but it's meant to be used on a set of form elements. Try this code and you will only get TypeError: $.serialize is not a function
How does even this work for you?
@MunkhdelgerTumenbayar see the answer date?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.