简体   繁体   English

jQuery AJAX,数组没有被序列化

[英]jQuery AJAX, array not getting serialized

I'm trying to post a multidimensional array using jQuery. 我正在尝试使用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? 我的理解是jQuery应该自动序列化数组?

Stringyfy the data before you send it to the server 在将数据发送到服务器之前对数据进行Stringyfy

Also it's a better practice to send the data as a Map.. 数据作为地图发送也是一种更好的做法

Instead of this 而不是这个

data: mainArray,

Try 尝试

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

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 ). 其次,你不能使用Array来创建哈希(理论上你可以,但它很糟糕,jQuery不理解这一点),你必须使用对象文字{}而不是Array ,只使用Array键使用Array (使用文字[]用于创建数组而不是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);
    }
});

PS : you can use $.param (convert js objects into query string) to figure out your mistakes PS :你可以使用$.param (将js对象转换为查询字符串)来找出你的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM