2013-03-06 73 views
0

我在這裏看到了很多這些問題,但似乎沒有解決我的問題。我有一個多維(嵌套)數組,我通過查詢填充。我想最終的陣列在AJAX的jQuery派:無法訪問Javascript多維數組

(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){ 

       } 
      }) 
     }); 

當我剛阿賈克斯之前登錄以上請求,它返回[[],[],[],[],[],[],[],[],[],[],[]],所以我知道一些工作,我只是認爲對象的整個內容將在服務器端可見。

此外,我已經把整個事情包裝在一個匿名函數中,因爲我認爲這有助於數組變量作用域。

+1

您使用的是PHP關聯數組語法與JavaScript數組。他們是不同的。 PHP關聯數組大致等於JavaScript對象。您需要將一些'['和']'改爲'{'和'}'。 – 2013-03-06 10:49:12

+1

您的數據將作爲POST數據發送到服務器(即以'&param = value'格式)。使用'console.log($。param(st))'來查看你的服務器將如何接收數據。 – 2013-03-06 10:51:00

回答

0

更改此
orderDetails['retailer'][i] = [];
這個
orderDetails['retailer'][i] = {};

如果你的item是不是你想要的參數i打電話,訪問它,這樣的功能:results.rows.item[i].ID