无法访问Javascript多维数组

时间:2013-03-06 10:45:22

标签: javascript jquery ajax multidimensional-array

我在这里看过很多这些问题,但似乎没有解决我的问题。我有一个多维(嵌套)数组,我通过查询填充。我希望通过AJAX jQuery发送最终数组:

(function() {
    var orderDetails = [];
    orderDetails['retailer'] = [];
    orderDetails['order'] = [];

    db.transaction(function(qry){
        qry.executeSql("SELECT * FROM retailers WHERE pending = '1' ", [], function(tx, results){
            len = results.rows.length; //if rows.length, means retailer is pending so add details to array. If no length, means retailer exists
            for (var i=0; i<len; i++){ 
                console.log('start '+i+' loop in retailers qry');
                orderDetails['retailer'][i] = [];
                orderDetails['retailer'][i]['localID'] = results.rows.item(i).ID;
                orderDetails['retailer'][i]['retailerName'] = results.rows.item(i).retailerName;
                orderDetails['retailer'][i]['address'] = results.rows.item(i).address;
                orderDetails['retailer'][i]['postcode'] = results.rows.item(i).postcode;
                console.log('finish '+i+' loop in retailers qry');
            }               
        }, function(err){console.log(err)})     
    }

这就是我填充数组的方式,这是AJAX请求:

        function(){
            console.log('start orders qry success callback');
            //alert(orderDetails['retailer'][0]['localID']);
            var st = JSON.stringify(orderDetails['retailer']);
            console.log(st);


            $.ajax({//send retailer to server, bring back the ID of the retailer as it is on the server so we can insert it into the order
                type: "POST",
                cache: false,
                //async: false,
                url: "https://www.......processOrder.php",
                data: { orderType: 'saved', orderDetails: st},
                dataType: "json",
                success: function(result){

                } 
            })
        });

当我在ajax请求之前记录上面的内容时,它会返回[[],[],[],[],[],[],[],[],[],[],[]]所以我知道某些内容正在运行,我只是认为该对象的所有内容都可以在服务器端看到。

此外,我已将整个事件包装在一个匿名函数中,因为我认为这有助于数组变量范围。

1 个答案:

答案 0 :(得分:0)

将此orderDetails['retailer'][i] = [];更改为此orderDetails['retailer'][i] = {};

如果您的item不是您要使用参数i调用的功能,请按以下方式访问:results.rows.item[i].ID