2013-06-20 55 views
1

我需要將數據上傳到像這樣的URL:使用jQuery的崗位關聯數組

http://www.your_domain.com/checkout/cart/add?product=68&qty=1&super_attribute[528]=55&super_attribute[525]=56 

See here for Magento documentation on query string

我當前的代碼看起來是這樣,但它不工作。

var myObject = { 
    super_attribute: {configOptionSuperAttribute: configOption} 
}; 

     jQuery.post(productAddToCartURL, { product: productId, qty: qty, super_attribute: jQuery.param(myObject) }) 
     .done(function(data) { 
      alert("Data Loaded: " + data); 
     }); 

問題出在super_atribute []上。我如何使myObject兼容?

感謝您的幫助!

+0

'configOption'的價值是什麼?它是一個數組還是對象?它看起來怎樣?數據使用'jQuery.param'(http://api.jquery.com/jQuery.param/)進行序列化。查看文檔以瞭解如何構建對象。 –

+0

當你說「它不工作」時,你是什麼意思?您是否收到JavaScript錯誤?電話是否到達服務器?完成的回調沒有被解僱? – Jacob

+0

你真的在你的代碼中使用了'jQuery.param'嗎?或者剛纔我提到它?你不應該明確地調用它,它由jQuery在內部調用來序列化整個數據對象。更新中的'myObject'在我的回答中看起來像'configOption'。不要讓它比它更復雜;) –

回答

3

根據jQuery.param(這是內部使用序列化的數據),你的數據應該是這樣的:

jQuery.post(
    productAddToCartURL, 
    {product: productId, qty: qty, super_attribute: configOption}, 
    function() { ... } 
); 

其中configOption是的對象表格

var configOption = { 
    528: 55, 
    525: 56 
}; 
+0

對象文字很棒。涼。 – blackhawk

1

我不知道你是否錯誤地粘貼了你的代碼,但是你的JavaScript有語法錯誤。這是無效的,因爲你有一個未封閉的報價。也許你的意思是這樣的:

jQuery 
    .post(
     productAddToCartURL, 
     { product: productId, qty: qty, 'super_attribute[]': configOption }) 
    .done(function(data) { alert("Data Loaded: " + data); }); 

或者,也許這? (您super_attribute[]屬性名是怪異):

jQuery 
    .post(
     productAddToCartURL, 
     { product: productId, qty: qty, super_attribute: configOption }) 
    .done(function(data) { alert("Data Loaded: " + data); });