jquery.ajax发布数据问题

时间:2014-10-01 15:23:03

标签: jquery shopify

我已经创建了一个jsfiddle非工作的代码示例。对于具有javascript或jquery功能体验的人来说,这可能是一项微不足道的任务。

http://jsfiddle.net/xphtxkhf/8/

我正在尝试使用shopify ajax api将带有订单项的产品添加到我的购物车中,以便您知道。

我的shopify应用程序收到这样的json:

var json = '{"line_items":[{"variant_id":"123456789","quantity":4,"properties":{"Line 1":"Some Value 1","Line 2":"Some Value 2","Line 3":"Some Value 3"}}],"products":[{"variant_id":"987654321","quantity":1},{"variant_id":"456987123","quantity":4}],"attributes":[{"lookup":"lookup value"}]}';

这是我解析它的代码,并尝试为jquery.ajax帖子准备“数据”。在代码注释中,由于缺乏对javascript基础知识的理解,你会看到我在哪里难倒。我无法弄清楚如何将“数据”填充到发送到jquery.ajax的“params”中。

在代码中你会看到一个如何在jquery.post中使用数据的例子,但是使用jquery.ajax似乎数据需要是一个字符串?如果是这样,我将如何使用字符串,jquery.post示例进行模拟?无论如何,我在这一点上很困惑,我甚至不确定如何正确地提出这个问题。我一直在看json.stringify,但我现在还不确定。可能是一个非常简单的答案。

var queue = [];
var attributesQueue = [];
var data = [];
var properties = [];
var moveAlong;

console.log(json);

var obj = jQuery.parseJSON( json );

$.each(obj.line_items, function(index, element) {
    queue.push( { 
        type: 'line_item',
        variantId: element.variantId,
        quantity: parseInt(element.quantity) || 0,
        properties: // need to put properties here
    });
});
    
$.each(obj.products, function(index, element) {
    queue.push( { 
        type: 'product',
        variantId: element.variantId,
        quantity: parseInt(element.quantity) || 0
    });
});
    
$.each(obj.attributes, function(index, element) {
    attributesQueue.push( { 
        type: 'attribute',
        name: index,
        value: obj.attributes[index]
    });
});
    
moveAlong = function() { // adding products to a shopify cart via ajax api has to be queued
    if (queue.length) {
        var request = queue.shift();                
        
        if (request.type == 'line_item') {
            // need to put data together for jQuery.ajax params POST
            /*
            
            This is an example jQuery.post which works, but I need to use jQuery.ajax I believe for the callbacks and queue. How do I create data with the quantity, id, and properties that will be inserted into the params correctly?
            
            jQuery.post('/cart/add.js', {
              quantity: 4,
              id: 123456789,
              properties: {
                'Line 1': 'Some Value 1',
                'Line 2': 'Some Value 2',
                'Line 3': 'Some Value 3'
              }
            });
            
            */
        } else {
            // need to put data together for jQuery.ajax params POST
        }
        
        var params = {
            type: 'POST',
            url: '/cart/add.js',      
            data: data,
            dataType: 'json',
            success: function(data, textStatus) {moveAlong()},
            error: function(XMLHttpRequest, textStatus) {console.log('error occurred.')}
        };
        
        jQuery.ajax(params);
    } else {
        console.log('Finished');
    }  
};
    
moveAlong();
Example of json being passed to javascript to parse and queue

<pre>
{
    "line_items":
    [
        {
            "variant_id":"123456789",
            "quantity":4,
            "properties":
            {
                "Line 1":"Some Value 1",
                "Line 2":"Some Value 2",
                "Line 3":"Some Value 3"
            }
        }
    ],
    "products":
    [
        {
            "variant_id":"987654321",
            "quantity":1
        },
        {
            "variant_id":"456987123",
            "quantity":4
        }
    ],
    "attributes":
    [
        {
            "lookup":"lookup value"
        }
    ]
}
</pre>

0 个答案:

没有答案