Reputation: 3678
I get the following response from the server:
{"aster":"3","daffodil":"4","rose":"3","totalItems":10,"totalPrice":"31.90"} RESPONSE
and, I want to put it in the table that will look like this:
The problem is that I do not know how to get values (for the "Quantity" column). This is my code:
function processServerResponse(data) {
if (data.products.length > 0) {
$("#orderForm").hide();
$("#summaryForm").show();
var html = '';
$.each(data.products, function(key, value) {
html += "<tr><td>"+value.name+"</td><td>"+?????+"</td></tr>"
});
$(html).appendTo("tbody");
$("#totalItems").text(data.totalItems);
$("#totalPrice").text(data.totalPrice);
}
}
Before that, this is how my $.ajax looks like (it's bigger code, you don' have to read it out):
$("#orderForm button").click(function (e) {
e.preventDefault();
var formData = $("#orderForm").serialize();
$("#popup").show();
$("body *").not("#popup").css("opacity", 0.5);
$("input").prop("disabled", true);
$.ajax({
url: "http://localhost/",
type: "post",
data: formData,
dataType: "json",
dataFilter: function(data, dataType) {
primljeniOdgovor = $.parseJSON(data);
var cleanData = {
totalItems: primljeniOdgovor.totalItems,
totalPrice: primljeniOdgovor.totalPrice
};
delete primljeniOdgovor.totalItems;
delete primljeniOdgovor.totalPrice;
cleanData.products = [];
for (prop in primljeniOdgovor) {
cleanData.products.push({
name: prop,
quantity: data[prop]
})
}
return cleanData;
},
converters: {
"text json": function(data) {
return data;
}
},
success: function(data) {
processServerResponse(data);
},
complete: function() {
setTimeout(function() {
$("#popup").hide();
$("body *").not("#popup").css("opacity", 1);
$("input").prop("disabled", false);
}, 1500);
}
});
})
I get the name from that response by value.name , but I have no idea how to get that value (quantity)?
Upvotes: 2
Views: 326
Reputation: 1105
There is an error in your code.
It must be
cleanData.products.push({
name: prop,
quantity: primljeniOdgovor[prop] //not data[prop]
})
and not quantity: data[prop]
. data
is only an unparsed String object. primljeniOdgovor
is the parsed Object that contains the property values.
See http://jsfiddle.net/t7n2tafk/
Upvotes: 1