0

I'm trying to post a multidimensional array using jQuery. I have verified that immediately before sending the array, it contains the contents it is supposed to (by checking specific elements and alerting them).

However, when I send the request, it's sending this:

Array
(
    [undefined] => 
)

Here's the whole thing...

           var mainArray = new Array();
           $(".list").each(function(){
               var day = $(this).attr("id");
               var order = 1;
               $("#" + id + " li").each(function(){
                   var subArray = new Array();
                   var id = $(this).attr("id");
                   subArray["id"] = id;
                   subArray["order"] = order;
                   subArray["day"] = day;
                   mainArray.push(subArray);
                   order++;
               });
           });

           // This displays what I would expect
           alert(mainArray[0]['id']);
           alert(mainArray[1]['id']);
           alert(mainArray[2]['id']);
           alert(mainArray[3]['id']);

           // This doesn't work
           $.ajax({
                type: 'post',
                url: 'test2.php',
                data: mainArray,
                success: function(data) {
                    $("#test").html(data);
                }
            });

Any ideas? My understanding is that jQuery is supposed to serialize the array automatically?

6
  • Not sure that it is possible. I would use json map to do that, exemple to stringify your array : stackoverflow.com/questions/191881/… Commented Dec 19, 2012 at 20:47
  • No, jQuery serializes objects automatically, expecting them to be a one-level-deep map of url parameter key-value pairs. Commented Dec 19, 2012 at 20:53
  • possible duplicate of jQuery ajax, how to send JSON in stead of QueryString Commented Dec 19, 2012 at 20:55
  • @Bergi I wonder where did you find word "JSON" in this question? Commented Dec 19, 2012 at 21:06
  • @Bergi You are wrong, jQuery serializes objects with any-level-deep: $.param({ items: { name: "test", children: [{ name: "child 1"}, { name: "child 2" }] }}) Commented Dec 19, 2012 at 21:09

2 Answers 2

1

Your code is totally wrong!

At first, give your 2-dimensional array some name for example items (or whatever you want). Second, you can't use Array for creating hash (theoretically you can but it's bad and jQuery doesn't understand this), you have to use object literals {} instead of Array, use Array only with numeric keys (use literals [] for creating array instead of new Array). Your code:

var mainArray = [];
$(".list").each(function(){
   var day = $(this).attr("id");
   var order = 1;

   $("#" + id + " li").each(function(){
       var subArray = {};

       subArray["id"] = $(this).attr("id");
       subArray["order"] = order;
       subArray["day"] = day;

       mainArray.push(subArray);
       order++;
   });
});

$.ajax({
    type: 'post',
    url: 'test2.php',
    data: { items: mainArray },
    success: function(data) {
        $("#test").html(data);
    }
});

P.S.: you can use $.param (convert js objects into query string) to figure out your mistakes

Sign up to request clarification or add additional context in comments.

Comments

1

Stringyfy the data before you send it to the server

Also it's a better practice to send the data as a Map..

Instead of this

data: mainArray,

Try

data: { 'arr': JSON.stringify(mainArray) },

5 Comments

JSON.stringify... WTF? He sends data using a normal REST POST request. Why should he encode his data with JSON?
By the same token, why should he not?
@Beetroot-Beetroot Maybe because it's overhead, isn't it? I suppose author of this question uses PHP as a backend, so if he sends normal HTTP POST request all request's vars will be available over $_POST array, so he shouldn't parse incoming data with json_decode. You suggested case in which JS should convert data into JSON and then backend should decode this data. It's unnecessary work for FE and for BE. Also almost all scripting languages gives mechanism to work with HTTP requests from the box, so it's a good reason to use standard interface
@Serjio, Regarding FE/BE load, remember the standard mechanism imposes a load too. Without running tests I couldn't say whether JSON coding imposes more or less load but would guess any difference will be trivial either way. In conclusion, "WTF" is a bit strong. How about "WBNS" (Why Be Non-Standard?) :)
@Beetroot-Beetroot Your are right. It seems I was a bit strict-) I'm sorry for this. But "WTF" is suitable because there is no any reminder about JSON in the question. And the issue can be resolved easier without using another communication type between FE and BE.

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.