I am using a for loop to output returned JSON data onto a webpage using javascript, then I am trying to get the id of the item to send it to the API via an AJAX call but I have hardcoded the id in the AJAX, could someone tell me how to send the id dynamically in the AJAX function. Also, do I need the missing callback function in the AJAX (updateCartData),thank you in advance!
JAVASCRIPT
function getCartData(data){
for (var i = 0; i < data.items.length; i++) {
var id = data.items[i].id;
var title = data.items[i].title
var price = data.items[i].price
var quantity = data.items[i].quantity
var image = data.items[i].image
$("#result").append("<div class='column left'><img src="+ image + "width=100 height=100 style=padding:20px></div><div class='column middle' >" + " " + title + " " + price + "</div><div class='column right' ><input type=number min=1 max=10 step=1 value="+quantity+" maxlength=2 size=2 /></div>");
}}
var script = document.createElement('script');
script.src = 'https://example.com/cart.json?callback=getCartData'
document.getElementsByTagName('head')[0].appendChild(script);
JSON
attributes: {}
cart_level_discount_applications: []
currency: "EUR"
item_count: 2
items: Array(1)
0: {id: 26100757856320, properties: {…}, quantity: 2, variant_id: 26100757856320, key: "26100757856320:6a238690d3db493c4359175c62751c6e", …}
length: 1
__proto__: Array(0)
items_subtotal_price: 133800
note: null
original_total_price: 133800
requires_shipping: true
token: "1234567890"
total_discount: 0
total_price: 133800
total_weight: 8000
__proto__: Object
AJAX
$( window ).load(function() {
$(":input").bind('keyup mouseup', function () {
$.ajax({
url: "https://example.com/cart/change.json?callback=updateCartData",
jsonp: "updateCartData",
dataType: "jsonp",
data: {
id: "26100757856320",
quantity : 2
},
success: function(data) {
console.log("quantity has been updated");
}
});
});
});
When generating the inputs, add the item Id to the input. In your Ajax call, just use $(this).attr('id')
for the id and $(this).val()
for the quantity.
function getCartData(data) {
for (var i = 0; i < data.items.length; i++) {
var id = data.items[i].id;
var title = data.items[i].title
var price = data.items[i].price
var quantity = data.items[i].quantity
var image = data.items[i].image
$("#result").append("<div class='column left'><img src=" + image + "width=100 height=100 style=padding:20px></div><div class='column middle' >" + " " + title + " " + price + "</div><div class='column right' ><input id='" + id + "' type=number min=1 max=10 step=1 value=" + quantity + " maxlength=2 size=2 /></div>");
}
}
$(":input").bind('keyup mouseup', function() {
$.ajax({
url: "https://example.com/cart/change.json?callback=updateCartData",
jsonp: "updateCartData",
dataType: "jsonp",
data: {
id: $(this).attr('id'),
quantity: $(this).val()
},
success: function(data) {
console.log("quantity has been updated");
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.